.hpp

K

karthikbalaguru

Hi,

I have never used .hpp for header file. I came across '.hpp' in a
certain set of code.
I searched the net, but it has been stated that we can use
either .h/.hpp for header files.
Is there any reason behind using '.hpp' ?
What could be the advantage of using '.hpp' extension over '.h'
extension ?

Thx,
Karthik Balaguru
 
I

Ian Collins

karthikbalaguru said:
Hi,

I have never used .hpp for header file. I came across '.hpp' in a
certain set of code.
I searched the net, but it has been stated that we can use
either .h/.hpp for header files.

You can use what ever you like...
Is there any reason behind using '.hpp' ?
What could be the advantage of using '.hpp' extension over '.h'
extension ?
Possibly to differentiate C++ headers from C ones.
 
J

Jim Langston

karthikbalaguru said:
Hi,

I have never used .hpp for header file. I came across '.hpp' in a
certain set of code.
I searched the net, but it has been stated that we can use
either .h/.hpp for header files.
Is there any reason behind using '.hpp' ?
What could be the advantage of using '.hpp' extension over '.h'
extension ?

You can name a header file anything you like. MyFile.blah is just as
"legal" as MyFile.h or MyFile.hpp Even MyFile. with no extention is legal
(such as #include <string> which is string. no extention. Some
compilers/people may prefer to give c++ header files a .hpp extention to
differentiate them beteeen C headers. Some IDEs and such may not recognize
any type other than .h or .hpp but the include statement will still be
executed correctly.
 
S

Saeed Amrollahi

Hi,

I have never used .hpp for header file. I came across '.hpp' in a
certain set of code.
I searched the net, but it has been stated that we can use
either .h/.hpp for header files.
Is there any reason behind using '.hpp' ?
What could be the advantage of using '.hpp' extension over '.h'
extension ?

Thx,
Karthik Balaguru

Hi Karthik
The use of differnet extensions for header file and even source file
is dependent to implementation and programming environment. For
example in Visual C++ and .NET environemts the convention is .h for
headers and .cpp for sources. In older versions of UNIX environment,
compilers used .C (capital c) or .CC for sources and .h for headers.
Newer compilers like GCC supports vast extensions
like .h, .hpp, .C, .CXX (it is + with 45 degrees rotation), .C++. I
personally use (.h/.C++).
And finally, I think from point of Standard C++ view, there is no
advantage using one extension over another one.

Regards,
Saeed
 
A

Alf P. Steinbach

* karthikbalaguru:
Hi,

I have never used .hpp for header file. I came across '.hpp' in a
certain set of code.
I searched the net, but it has been stated that we can use
either .h/.hpp for header files.
Is there any reason behind using '.hpp' ?
What could be the advantage of using '.hpp' extension over '.h'
extension ?

I use [.hpp] for C++ headers that provide implementation (no separate
compilation needed), and that seems to be the convention also in Boost.

Cheers, & hth.,

- Alf
 
J

Juha Nieminen

karthikbalaguru said:
What could be the advantage of using '.hpp' extension over '.h'
extension ?

If you see a file with the extension '.h' you can't know if it's a
C header or a C++ header without looking at its contents (and even then
it might not be immediately obvious). You obviously can't use a C++
header in a C program, although in a few cases the opposite is true too.

More importantly, editor programs which support, for example, syntax
highlighting cannot know if it's a C or a C++ file without complex
deductions from the file contents. I assume that most such editors
simply always assume that it's one or the other, getting it wrong
sometimes, thus giving you wrong syntax highlighting.

Why use a confusing naming convention when you can use a clearer one?
What would be the advantage of using '.h' for C++ headers, given that
they can be easily confused with C headers?
 
R

Rui Maciel

karthikbalaguru said:
I have never used .hpp for header file. I came across '.hpp' in a
certain set of code.
I searched the net, but it has been stated that we can use
either .h/.hpp for header files.
Is there any reason behind using '.hpp' ?

It's the exact same reason why the C++ source files frequently sport
the ".cpp" extension.

What could be the advantage of using '.hpp' extension over '.h'
extension ?

I've used those extensions, along with ".c++" and ".h++". I've also seen
projects which adopted the ".cxx" and ".hxx" extensions.

To me, the single advantage is that just by looking at the file name it is
automatically possible to know that the code it contains is C or C++. That
is particularly useful if a project handles both C and C++ modules.

On the other hand, there are quite a few artificial disadvantages. There are
quite a few IDEs and other software that, although were written to handle
C++ programming, do not accept anything beyond the popular ".h" and ".cpp"
extensions or even breaking miserably if they stumble on some other
extension.


Rui Maciel
 
P

Pete Becker

If you see a file with the extension '.h' you can't know if it's a
C header or a C++ header without looking at its contents

Why would you look at its contents? Did you lose the documentation?
 
J

James Kanze

It's the exact same reason why the C++ source files frequently sport
the ".cpp" extension.

The extension on the source file is used by the compiler, to
determine whether to compile the file as C++ or as C. The
extension on the include file is ignored by the compiler.

Of course, with all of the compilers I know, you can override
this. Historically, .cc seems to be the most frequent extension
for C++ sources in a Unix environment, but older versions of
VC++ didn't recognize it, so I regularly used the /Tp option
when I compiled; it was easier to add this in the makefile than
to rename all of my files. (Historically, of course, the
extension for C++ sources was .C, as opposed to .c for C
sources. This convention broke down as soon as C++ was ported
to OS's which didn't have case sensitive filenames, however.)
I've used those extensions, along with ".c++" and ".h++". I've
also seen projects which adopted the ".cxx" and ".hxx"
extensions.

The problem is precisely that the convention (established by the
creator of the language) was .C. When different people ported
to systems which didn't have case sensitive filenames, they had
to create a new convention. The most common to begin with seem
to have been .cc and .cxx, but Microsoft (and maybe Borland
before them) adopted .cpp, so that rapidly became the most
common under Windows. All three are acceptable to all modern
compilers I know.

For whatever reason, Stroustrup didn't distinguish between C and
C++ header files: .h was the convention for C, and it was also
the extention for the earliest C++ headers. Many people (myself
included) have felt that it would be better to distinguish, and
simply adopted the same convention as for source files,
replacing the c with an h: .H in the far distant past, then .hh
or .hxx, and finally .hpp under Windows. In the case of include
files, however, the compiler doesn't care. In the past, .ihh
was frequent (at least in the places I worked) for headers which
defined the inline functions (and which were included from
another header); along the same lines, I have long used .lhh (in
addition to .hh) for local headers, which were used when
compiling a component, but which weren't exported. And I've
also adopted a convention used by g++ (and others), using .tcc
for template sources (which I used to put in the .ihh files).

Another poster mentionned IDE's: my editors are configured so
that any filename matching *.*hh or *.*cc is treated as C++; *.h
and *.c are treated as C. And of course, it would be trivial to
change the configuration so that it recognized *.hpp and *.cpp
as well.

The important thing is to establish a convention, document it,
and to ensure that everyone knows it.

Pete Becker correctly pointed out that you shouldn't be using an
include file without having read its documentation. Curiously
enough, however, the documentation I've seen rarely mentions the
programming language, however---one assumes that everything in
Boost is C++, and everything in Posix is C, but I don't think
I've seen it documented. And while this is definitly a safe
(and obvious) assumption in those two cases, I'm not sure it's
that obvious for other libraries. And of course, you might want
other programs (e.g. the editor) to behave differently depending
on the language, and other programs don't read the
documentation.
To me, the single advantage is that just by looking at the
file name it is automatically possible to know that the code
it contains is C or C++. That is particularly useful if a
project handles both C and C++ modules.

I agree that it's useful, although you still don't know whether
the .h is C only, or C and C++.
On the other hand, there are quite a few artificial
disadvantages. There are quite a few IDEs and other software
that, although were written to handle C++ programming, do not
accept anything beyond the popular ".h" and ".cpp" extensions
or even breaking miserably if they stumble on some other
extension.

Are you kidding? I've never encountered one.
 
P

Pete Becker

The problem is precisely that the convention (established by the
creator of the language) was .C. When different people ported
to systems which didn't have case sensitive filenames, they had
to create a new convention. The most common to begin with seem
to have been .cc and .cxx, but Microsoft (and maybe Borland
before them) adopted .cpp, so that rapidly became the most
common under Windows. All three are acceptable to all modern
compilers I know.

Yes, Borland used .cpp. That's where Microsoft got it from.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top