Correct C++ file extension [duplicate] - c++

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Correct C++ code file extension? .cc vs .cpp
.c vs .cc vs. .cpp vs .hpp vs .h vs .cxx
I have written C++ code in files with extension .cpp, .cc or .cxx
Can anyone explain that what is the difference between these three and which one is the best ( is that platform dependent ) and why?
I am currently working on cygwin.

It's a matter of code conventions. These extensions are all interchangeable (as far as platforms are concerned). If you participate in an existing project, you should adhere to this project's conventions, otherwise just choose whatever you like.

The .cpp extension used to mean C PreProcessor but nowadays is often used for C++. However, I'd avoid it due to the older meaning.
.c++ is pretty obvious, but some people avoid it since the plus might be a special character in some shells which you would need to escape, IIRC.
.cxx is the "compromise", think rotating a plus by 45 degrees.
I've not seen .cc very often, YMMV.
But at the end of the day you could pick whichever extension you like, even .c-plus-plus, since the compiler doesn't care. So go with the one you like the best. As always with coding, if you're working in an existing project stick to the convention that's already established there.

They are just different conventions; none is better than the others or has any special meaning relative to the others. Often people will name headers using the same associated convention: .h or .hxx or .hpp. Just be consistent, and it doesn't matter which one you use.

There is no difference at all. Its not platform dependent. You can use any of them.

Related

Is it always a good idea to create .h and .cpp files together? [duplicate]

I've never really understood why C++ needs a separate header file with the same functions as in the .cpp file. It makes creating classes and refactoring them very difficult, and it adds unnecessary files to the project. And then there is the problem with having to include header files, but having to explicitly check if it has already been included.
C++ was ratified in 1998, so why is it designed this way? What advantages does having a separate header file have?
Follow up question:
How does the compiler find the .cpp file with the code in it, when all I include is the .h file? Does it assume that the .cpp file has the same name as the .h file, or does it actually look through all the files in the directory tree?
Some people consider header files an advantage:
It is claimed that it enables/enforces/allows separation of interface and implementation -- but usually, this is not the case. Header files are full of implementation details (for example member variables of a class have to be specified in the header, even though they're not part of the public interface), and functions can, and often are, defined inline in the class declaration in the header, again destroying this separation.
It is sometimes said to improve compile-time because each translation unit can be processed independently. And yet C++ is probably the slowest language in existence when it comes to compile-times. A part of the reason is the many many repeated inclusions of the same header. A large number of headers are included by multiple translation units, requiring them to be parsed multiple times.
Ultimately, the header system is an artifact from the 70's when C was designed. Back then, computers had very little memory, and keeping the entire module in memory just wasn't an option. A compiler had to start reading the file at the top, and then proceed linearly through the source code. The header mechanism enables this. The compiler doesn't have to consider other translation units, it just has to read the code from top to bottom.
And C++ retained this system for backwards compatibility.
Today, it makes no sense. It is inefficient, error-prone and overcomplicated. There are far better ways to separate interface and implementation, if that was the goal.
However, one of the proposals for C++0x was to add a proper module system, allowing code to be compiled similar to .NET or Java, into larger modules, all in one go and without headers. This proposal didn't make the cut in C++0x, but I believe it's still in the "we'd love to do this later" category. Perhaps in a TR2 or similar.
You seem to be asking about separating definitions from declarations, although there are other uses for header files.
The answer is that C++ doesn't "need" this. If you mark everything inline (which is automatic anyway for member functions defined in a class definition), then there is no need for the separation. You can just define everything in the header files.
The reasons you might want to separate are:
To improve build times.
To link against code without having the source for the definitions.
To avoid marking everything "inline".
If your more general question is, "why isn't C++ identical to Java?", then I have to ask, "why are you writing C++ instead of Java?" ;-p
More seriously, though, the reason is that the C++ compiler can't just reach into another translation unit and figure out how to use its symbols, in the way that javac can and does. The header file is needed to declare to the compiler what it can expect to be available at link time.
So #include is a straight textual substitution. If you define everything in header files, the preprocessor ends up creating an enormous copy and paste of every source file in your project, and feeding that into the compiler. The fact that the C++ standard was ratified in 1998 has nothing to do with this, it's the fact that the compilation environment for C++ is based so closely on that of C.
Converting my comments to answer your follow-up question:
How does the compiler find the .cpp file with the code in it
It doesn't, at least not at the time it compiles the code that used the header file. The functions you're linking against don't even need to have been written yet, never mind the compiler knowing what .cpp file they'll be in. Everything the calling code needs to know at compile time is expressed in the function declaration. At link time you will provide a list of .o files, or static or dynamic libraries, and the header in effect is a promise that the definitions of the functions will be in there somewhere.
C++ does it that way because C did it that way, so the real question is why did C do it that way? Wikipedia speaks a little to this.
Newer compiled languages (such as
Java, C#) do not use forward
declarations; identifiers are
recognized automatically from source
files and read directly from dynamic
library symbols. This means header
files are not needed.
To my (limited - I'm not a C developer normally) understanding, this is rooted in C. Remember that C does not know what classes or namespaces are, it's just one long program. Also, functions have to be declared before you use them.
For example, the following should give a compiler error:
void SomeFunction() {
SomeOtherFunction();
}
void SomeOtherFunction() {
printf("What?");
}
The error should be that "SomeOtherFunction is not declared" because you call it before it's declaration. One way of fixing this is by moving SomeOtherFunction above SomeFunction. Another approach is to declare the functions signature first:
void SomeOtherFunction();
void SomeFunction() {
SomeOtherFunction();
}
void SomeOtherFunction() {
printf("What?");
}
This lets the compiler know: Look somewhere in the code, there is a function called SomeOtherFunction that returns void and does not take any parameters. So if you encouter code that tries to call SomeOtherFunction, do not panic and instead go looking for it.
Now, imagine you have SomeFunction and SomeOtherFunction in two different .c files. You then have to #include "SomeOther.c" in Some.c. Now, add some "private" functions to SomeOther.c. As C does not know private functions, that function would be available in Some.c as well.
This is where .h Files come in: They specify all the functions (and variables) that you want to 'Export' from a .c file that can be accessed in other .c files. That way, you gain something like a Public/Private scope. Also, you can give this .h file to other people without having to share your source code - .h files work against compiled .lib files as well.
So the main reason is really for convenience, for source code protection and to have a bit of decoupling between the parts of your application.
That was C though. C++ introduced Classes and private/public modifiers, so while you could still ask if they are needed, C++ AFAIK still requires declaration of functions before using them. Also, many C++ Developers are or were C devleopers as well and took over their concepts and habits to C++ - why change what isn't broken?
First advantage: If you don't have header files, you would have to include source files in other source files. This would cause the including files to be compiled again when the included file changes.
Second advantage: It allows sharing the interfaces without sharing the code between different units (different developers, teams, companies etc..)
The need for header files results from the limitations that the compiler has for knowing about the type information for functions and or variables in other modules. The compiled program or library does not include the type information required by the compiler to bind to any objects defined in other compilation units.
In order to compensate for this limitation, C and C++ allow for declarations and these declarations can be included into modules that use them with the help of the preprocessor's #include directive.
Languages like Java or C# on the other hand include the information necessary for binding in the compiler's output (class-file or assembly). Hence, there is no longer a need for maintaining standalone declarations to be included by clients of a module.
The reason for the binding information not being included in the compiler output is simple: it is not needed at runtime (any type checking occurs at compile time). It would just waste space. Remember that C/C++ come from a time where the size of an executable or library did matter quite a bit.
Well, C++ was ratified in 1998, but it had been in use for a lot longer than that, and the ratification was primarily setting down current usage rather than imposing structure. And since C++ was based on C, and C has header files, C++ has them too.
The main reason for header files is to enable separate compilation of files, and minimize dependencies.
Say I have foo.cpp, and I want to use code from the bar.h/bar.cpp files.
I can #include "bar.h" in foo.cpp, and then program and compile foo.cpp even if bar.cpp doesn't exist. The header file acts as a promise to the compiler that the classes/functions in bar.h will exist at run-time, and it has everything it needs to know already.
Of course, if the functions in bar.h don't have bodies when I try to link my program, then it won't link and I'll get an error.
A side-effect is that you can give users a header file without revealing your source code.
Another is that if you change the implementation of your code in the *.cpp file, but do not change the header at all, you only need to compile the *.cpp file instead of everything that uses it. Of course, if you put a lot of implementation into the header file, then this becomes less useful.
C++ was designed to add modern programming language features to the C infrastructure, without unnecessarily changing anything about C that wasn't specifically about the language itself.
Yes, at this point (10 years after the first C++ standard and 20 years after it began seriously growing in usage) it is easy to ask why doesn't it have a proper module system. Obviously any new language being designed today would not work like C++. But that isn't the point of C++.
The point of C++ is to be evolutionary, a smooth continuation of existing practise, only adding new capabilities without (too often) breaking things that work adequately for its user community.
This means that it makes some things harder (especially for people starting a new project), and some things easier (especially for those maintaining existing code) than other languages would do.
So rather than expecting C++ to turn into C# (which would be pointless as we already have C#), why not just pick the right tool for the job? Myself, I endeavour to write significant chunks of new functionality in a modern language (I happen to use C#), and I have a large amount of existing C++ that I am keeping in C++ because there would be no real value in re-writing it all. They integrate very nicely anyway, so it's largely painless.
It doesn't need a separate header file with the same functions as in main. It only needs it if you develop an application using multiple code files and if you use a function that was not previously declared.
It's really a scope problem.
C++ was ratified in 1998, so why is it designed this way? What advantages does having a separate header file have?
Actually header files become very useful when examining programs for the first time, checking out header files(using only a text editor) gives you an overview of the architecture of the program, unlike other languages where you have to use sophisticated tools to view classes and their member functions.
If you want the compiler to find out symbols defined in other files automatically, you need to force programmer to put those files in predefined locations (like Java packages structure determines folders structure of the project). I prefer header files. Also you would need either sources of libraries you use or some uniform way to put information needed by compiler in binaries.
I think the real (historical) reason behind header files was making like easier for compiler developers... but then, header files do give advantages.
Check this previous post for more discussions...
Well, you can perfectly develop C++ without header files. In fact some libraries that intensively use templates does not use the header/code files paradigm (see boost). But In C/C++ you can not use something that is not declared. One practical way to
deal with that is to use header files. Plus, you gain the advantage of sharing interface whithout sharing code/implementation. And I think it was not envisionned by the C creators : When you use shared header files you have to use the famous :
#ifndef MY_HEADER_SWEET_GUARDIAN
#define MY_HEADER_SWEET_GUARDIAN
// [...]
// my header
// [...]
#endif // MY_HEADER_SWEET_GUARDIAN
that is not really a language feature but a practical way to deal with multiple inclusion.
So, I think that when C was created, the problems with forward declaration was underestimated and now when using a high level language like C++ we have to deal with this sort of things.
Another burden for us poor C++ users ...

What is the difference between .cc and .cpp file suffix? [duplicate]

This question already has answers here:
C++ code file extension? What is the difference between .cc and .cpp [closed]
(17 answers)
Closed 9 years ago.
What is the difference between .cc and .cpp file extensions?
From Google, I learned that they are both from the C++ language, but I am unsure of differences between them.
Conventions.
Historically, the suffix for a C++ source file was .C.
This caused a few problems the first time C++ was ported
to a system where case wasn't significant in the filename.
Different users adopted different solutions: .cc,
.cpp, .cxx and possibly others. Today, outside of the Unix
world, it's mostly .cpp. Unix seems to use .cc more often.
For headers, the situation is even more confusing: for whatever
reasons, the earliest C++ authors decided not to distinguish
between headers for C and for C++, and used .h.
This doesn't cause any problems if there is no C in the project, but when you
start having to deal with both, it's usually a good idea to
distinguish between the headers which can be used in C (.h)
and those which cannot (.hh or .hpp).
In addition, in C++, a lot of users (including myself) prefer keeping the template
sources and the inline functions in a separate file. Which,
while strictly speaking a header file, tends to get yet another
set of conventions (.inl, .tcc and probably a lot of
others).
In the case of headers it makes absolutely no difference to the compiler.
In the case of source files different endings will cause the compiler to assume a different
language. But this can normally be overridden, and I used .cc
with VC++ long before VC++ recognized it as C++.
There is no difference. They're exactly the same.
Technically for the compiler there is no difference. However, some compilers and/or build systems will guess how to compile your files based on the extension and may or may not detect "cc" (or "cpp" but that is more rare I guess) as a c++ file.
Actually it all depends on what you and your compiler prefer. There is no difference between them at all.

C++ header file [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
In C++ why have header files and cpp files?
Why in C++ is there a .h and .cpp and not only one file like c# and Java ?
For historical reasons. Specifically, for compatibility with C. That language was originally designed to run on (for 70s standards) low-end machines; header files were (and often still are) substituted inline by a separate program, to keep the memory use of the compiler down. It still helps to keep libraries small.
Because c# and java do not require forward declarations. C++ requires the forward declarations.
C++ has a pre-processor that it inherited from C. The pre-processor has many interesting features, but one of the things that it is used for is to structure the code into headers and source files.
Structuring the code into headers and source files allows you to determine which parts of the code are visible to other source files (i.e. the parts you put in the header) and which ones aren't.
It also means that you don't need any special tools to know which classes are available to you, as you would if you had import in stead of #include: you have part of the source code to work with in stead (which you can read with any text editor).
The advantages notwithstanding, the pre-processor is really a legacy from C++'s parent, C, and has survived the evolution of C++ almost entirely in-tact.

What objective reasons for coding without headers in C or C++ are there? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Coding C++ without headers, best practices?
Inspired by this question - the OP just finds the concept of headers confusing and depressing there, it's his subjective experience.
Now are there any objective reasons and scenarios for avoiding headers when coding in C or C++?
There are no objective reasons, only irrational ones borne of subjective bias induced by experiences with other languages for which headers don't exist as a concept.
The compilation unit is a fundamental concept in C and C++ compilers. A compilation unit must have access to all declarations at the source code level in order to access functionality provided by other compilation units, and the only logically coherent way to ensure that publishers and consumers of functionality see the declarations is for them to share the source code for those declarations. Headers are the natural way to achieve this.
Being a bit more technical about it, headers are not a C (or C++) concept, per se. They are preprocessor concept. When processing a source file, whenever the preprocessor encounters a #include directive, it replaces the directive with the entire contents of the header being referred to, recursively expanding any #include directives in the header itself. By the time the compiler proper kicks in, it sees nothing but a single "file" containing all the declarations it needs to do its job.
The key point is that, for better or worse, headers are the standard way to organise complex C and C++ code bases. You can play fun and games with #ifdefs to put both "header" and implementation in one source file, but that works against every tool-chain in the universe, and means that preprocessors have far more work to do, thus dramatically slowing down your builds.
None at all, it's just a bad idea.
The only objective reason would be if your C compiler could only include an extremely limited number of files over the course of a compilation, or the system you are working on having an extremely limited number of inodes.
#includeing a header file tells the compiler, quite literally, "copy-paste some text here." Now perhaps your question is really asking, "are there any misuses for header files?" (and there certainly are), but as written, it makes as little sense as, "are there any objective reasons to avoid source files?"
I found header files great before I worked with java. Now I find them annoying, because it is code duplication, you have to keep 2 files in sync.
Of course, if you have a tool which builds the header dynamically, it's not that bad. Are there such tools?
No, header files are an essential tool and cannot be removed in the language as it is. That doesn't mean that they don't completely suck and shouldn't be removed ASAP.
I have experienced one good reason for avoiding headers in C: distributing an open source application as a single .c file.
This is the distribution mode of dcraw, an ANSI-C cross-platform software for converting digital camera RAW files.

*.h or *.hpp for your class definitions

I've always used a *.h file for my class definitions, but after reading some boost library code, I realised they all use *.hpp. I've always had an aversion to that file extension, I think mainly because I'm not used to it.
What are the advantages and disadvantages of using *.hpp over *.h?
Here are a couple of reasons for having different naming of C vs C++ headers:
Automatic code formatting, you might have different guidelines for formatting C and C++ code. If the headers are separated by extension you can set your editor to apply the appropriate formatting automatically
Naming, I've been on projects where there were libraries written in C and then wrappers had been implemented in C++. Since the headers usually had similar names, i.e. Feature.h vs Feature.hpp, they were easy to tell apart.
Inclusion, maybe your project has more appropriate versions available written in C++ but you are using the C version (see above point). If headers are named after the language they are implemented in you can easily spot all the C-headers and check for C++ versions.
Remember, C is not C++ and it can be very dangerous to mix and match unless you know what you are doing. Naming your sources appropriately helps you tell the languages apart.
I use .hpp because I want the user to differentiate what headers are C++ headers, and what headers are C headers.
This can be important when your project is using both C and C++ modules: Like someone else explained before me, you should do it very carefully, and its starts by the "contract" you offer through the extension
.hpp : C++ Headers
(Or .hxx, or .hh, or whatever)
This header is for C++ only.
If you're in a C module, don't even try to include it. You won't like it, because no effort is done to make it C-friendly (too much would be lost, like function overloading, namespaces, etc. etc.).
.h : C/C++ compatible or pure C Headers
This header can be included by both a C source, and a C++ source, directly or indirectly.
It can included directly, being protected by the __cplusplus macro:
Which mean that, from a C++ viewpoint, the C-compatible code will be defined as extern "C".
From a C viewpoint, all the C code will be plainly visible, but the C++ code will be hidden (because it won't compile in a C compiler).
For example:
#ifndef MY_HEADER_H
#define MY_HEADER_H
#ifdef __cplusplus
extern "C"
{
#endif
void myCFunction() ;
#ifdef __cplusplus
} // extern "C"
#endif
#endif // MY_HEADER_H
Or it could be included indirectly by the corresponding .hpp header enclosing it with the extern "C" declaration.
For example:
#ifndef MY_HEADER_HPP
#define MY_HEADER_HPP
extern "C"
{
#include "my_header.h"
}
#endif // MY_HEADER_HPP
and:
#ifndef MY_HEADER_H
#define MY_HEADER_H
void myCFunction() ;
#endif // MY_HEADER_H
I always considered the .hpp header to be a sort of portmanteau of .h and .cpp files...a header which contains implementation details as well.
Typically when I've seen (and use) .hpp as an extension, there is no corresponding .cpp file. As others have said, this isn't a hard and fast rule, just how I tend to use .hpp files.
It does not matter which extension you use. Either one is OK.
I use *.h for C and *.hpp for C++.
EDIT [Added suggestion from Dan Nissenbaum]:
By one convention, .hpp files are used when the prototypes are defined in the header itself. Such definitions in headers are useful in case of templates, since the compiler generates the code for each type only on template instantiation. Hence, if they are not defined in header files, their definitions will not be resolved at link time from other compilation units. If your project is a C++ only project that makes heavy use of templates, this convention will be useful.
Certain template libraries that adhere to this convention provide headers with .hpp extensions to indicate that they do not have corresponding .cpp files.
another convention is to use .h for C headers and .hpp for C++; a good example would be the boost library.
Quote from Boost FAQ,
File extensions communicate the "type" of the file, both to humans
and to computer programs. The '.h' extension is used for C header
files, and therefore communicates the wrong thing about C++ header
files. Using no extension communicates nothing and forces inspection
of file contents to determine type. Using '.hpp' unambiguously
identifies it as C++ header file, and works well in actual practice.
(Rainer Deyke)
I'm answering this as an reminder, to give point to my comment(s) on "user1949346" answer to this same OP.
So as many already answered: either way is fine.
Followed by emphasizes of their own impressions.
Introductory, as also in the previous named comments stated, my opinion is C++ header extensions are proposed to be .h if there is actually no reason against it.
Since the ISO/IEC documents use this notation of header files and no string matching to .hpp even occurs in their language documentations about C++.
But I'm now aiming for an approvable reason WHY either way is ok, and especially why it's not subject of the language it self.
So here we go.
The C++ documentation (I'm actually taking reference from the version N3690) defines that a header has to conform to the following syntax:
2.9 Header names
header-name:
< h-char-sequence >
" q-char-sequence "
h-char-sequence:
h-char
h-char-sequence h-char
h-char:
any member of the source character set except new-line and >
q-char-sequence:
q-char
q-char-sequence q-char
q-char:
any member of the source character set except new-line and "
So as we can extract from this part, the header file name may be anything that is valid in the source code, too. Except containing '\n' characters and depending on if it is to be included by <> it is not allowed to contain a >.
Or the other way if it is included by ""-include it is not allowed to contain a ".
In other words: if you had a environment supporting filenames like prettyStupidIdea.>, an include like:
#include "prettyStupidIdea.>"
would be valid, but:
#include <prettyStupidIdea.>>
would be invalid. The other way around the same.
And even
#include <<.<>
would be a valid includable header file name.
Even this would conform to C++, it would be a pretty pretty stupid idea, tho.
And that's why .hpp is valid, too.
But it's not an outcome of the committees designing decisions for the language!
So discussing about to use .hpp is same as doing it about .cc, .mm or what ever else I read in other posts on this topic.
I have to admit I have no clue where .hpp came from1, but I would bet an inventor of some parsing tool, IDE or something else concerned with C++ came to this idea to optimize some internal processes or just to invent some (probably even for them necessarily) new naming conventions.
But it is not part of the language.
And whenever one decides to use it this way. May it be because he likes it most or because some applications of the workflow require it, it never2 is a requirement of the language. So whoever says "the pp is because it is used with C++", simply is wrong in regards of the languages definition.
C++ allows anything respecting the previous paragraph.
And if there is anything the committee proposed to use, then it is using .h since this is the extension sued in all examples of the ISO document.
Conclusion:
As long you don't see/feel any need of using .h over .hpp or vise versa, you shouldn't bother. Because both would be form a valid header name of same quality in respect to the standard. And therefore anything that REQUIRES you to use .h or .hpp is an additional restriction of the standard which could even be contradicting with other additional restrictions not conform with each other. But as OP doesn't mention any additional language restriction, this is the only correct and approvable answer to the question
"*.h or *.hpp for your class definitions" is:
Both are equally correct and applicable as long as no external restrictions are present.
1From what I know, apparently, it is the boost framework that came up with that .hpp extension.
2Of course I can't say what some future versions will bring with it!
I've recently started using *.hpp for c++ headers.
The reason is that I use emacs as my primary editor and it enters automatically into c-mode when you load a *.h file and into c++-mode when you load a *.hpp file.
Apart that fact I see no good reasons for choosing *.h over *.hpp or vice-versa.
You can call your includes whatever you like.
Just need to specify that full name in the #include.
I suggest that if you work with C to use .h and when with C++ to use .hpp.
It is in the end just a convention.
I prefer .hpp for C++ to make it clear to both editors and to other programmers that it is a C++ header rather than a C header file.
Fortunately, it is simple.
You should use the .hpp extension if you're working with C++ and you should use .h for C or mixing C and C++.
Codegear C++Builder uses .hpp for header files automagically generated from Delphi source files, and .h files for your "own" header files.
So, when I'm writing a C++ header file I always use .h.
C++ ("C Plus Plus") makes sense as .cpp
Having header files with a .hpp extension doesn't have the same logical flow.
Bjarne Stroustrup and Herb Sutter have a statement to this question in their C++ Core guidelines found on: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#S-source which is also refering to the latest changes in the standard extension (C++11, C++14, etc.)
SF.1: Use a .cpp suffix for code files and .h for interface files if your
Y project doesn't already follow another convention
Reason
It's a longstanding convention. But consistency is more important, so if
your project uses something else, follow that.
Note
This convention reflects a common use pattern: Headers are more often shared
with C to compile as both C++ and C, which typically uses .h, and it's
easier to name all headers .h instead of having different extensions for
just those headers that are intended to be shared with C. On the other hand,
implementation files are rarely shared with C and so should typically be
distinguished from .c files, so it's normally best to name all C++
implementation files something else (such as .cpp).
The specific names .h and .cpp are not required (just recommended as a
default) and other names are in widespread use. Examples are .hh, .C, and
.cxx. Use such names equivalently. In this document, we refer to .h and .cpp > as a shorthand for header and implementation files, even though the actual
extension may be different.
Your IDE (if you use one) may have strong opinions about suffices.
I'm not a big fan of this convention because if you are using a popular library like boost, your consistency is already broken and you should better use .hpp.
In one of my jobs in the early 90's, we used .cc and .hh for source and header files respectively. I still prefer it over all the alternatives, probably because it's easiest to type.
It is easy for tools and humans to differentiate something. That's it.
In conventional use (by boost, etc), .hpp is specifically C++ headers. On the other hand, .h is for non-C++-only headers (mainly C). To precisely detect the language of the content is generally hard since there are many non-trivial cases, so this difference often makes a ready-to-use tool easy to write. For humans, once get the convention, it is also easy to remember and easy to use.
However, I'd point out the convention itself does not always work, as expected.
It is not forced by the specification of languages, neither C nor C++. There exist many projects which do not follow the convention. Once you need to merge (to mix) them, it can be troublesome.
.hpp itself is not the only choice. Why not .hh or .hxx? (Though anyway, you usually need at least one conventional rule about filenames and paths.)
I personally use both .h and .hpp in my C++ projects. I don't follow the convention above because:
The languages used by each part of the projects are explicitly documented. No chance to mix C and C++ in same module (directory). Every 3rdparty library is required to conforming to this rule.
The conformed language specifications and allowed language dialects used by the projects are also documented. (In fact, I even document the source of the standard features and bug fix (on the language standard) being used.) This is somewhat more important than distinguishing the used languages since it is too error-prone and the cost of test (e.g. compiler compatibility) may be significant (complicated and time-consuming), especially in a project which is already in almost pure C++. Filenames are too weak to handle this.
Even for the same C++ dialect, there may be more important properties suitable to the difference. For example, see the convention below.
Filenames are essentially pieces of fragile metadata. The violation of convention is not so easy to detect. To be stable dealing the content, a tool should eventually not only depend on names. The difference between extensions is only a hint. Tools using it should also not be expected behave same all the time, e.g. language-detecting of .h files on github.com. (There may be something in comments like shebang for these source files to be better metadata, but it is even not conventional like filenames, so also not reliable in general.)
I usually use .hpp on C++ headers and the headers should be used (maintained) in a header-only manner, e.g. as template libraries. For other headers in .h, either there is a corresponding .cpp file as implementation, or it is a non-C++ header. The latter is trivial to differentiate through the contents of the header by humans (or by tools with explicit embedded metadata, if needed).
As many here have already mentioned, I also prefer using .hpp for header-only libraries that use template classes/functions. I prefer to use .h for header files accompanied by .cpp source files or shared or static libraries.
Most of the libraries I develop are template based and thus need to be header only, but when writing applications I tend to separate declaration from implementation and end up with .h and .cpp files
I use .h because that's what Microsoft uses, and what their code generator creates. No need to go against the grain.
In "The C++ Programming Language, Third Edition by Bjarne Stroustrup", the nÂș1 must-read C++ book, he uses *.h. So I assume the best practice is to use *.h.
However, *.hpp is fine as well!
There is no advantage to any particular extension, other than that one may have a different meaning to you, the compiler, and/or your tools. header.h is a valid header. header.hpp is a valid header. header.hh is a valid header. header.hx is a valid header. h.header is a valid header. this.is.not.a.valid.header is a valid header in denial. ihjkflajfajfklaf is a valid header. As long as the name can be parsed properly by the compiler, and the file system supports it, it's a valid header, and the only advantage to its extension is what one reads into it.
That being said, being able to accurately make assumptions based on the extension is very useful, so it would be wise to use an easily-understandable set of rules for your header files. Personally, I prefer to do something like this:
If there are already any established guidelines, follow them to prevent confusion.
If all source files in the project are for the same language, use .h. There's no ambiguity.
If some headers are compatible with multiple languages, while others are only compatible with a single language, extensions are based on the most restrictive language that a header is compatible with. A header compatible with C, or with both C & C++, gets .h, while a header compatible with C++ but not C gets .hpp or .hh or something of the sort.
This, of course, is but one of many ways to handle extensions, and you can't necessarily trust your first impression even if things seem straightforward. For example, I've seen mention of using .h for normal headers, and .tpp for headers that only contain definitions for templated class member functions, with .h files that define templated classes including the .tpp files that define their member functions (instead of the .h header directly containing both the function declaration and the definition). For another example, a good many people always reflect the header's language in its extension, even when there's no chance of ambiguity; to them, .h is always a C header and .hpp (or .hh, or .hxx, etc.) is always a C++ header. And yet again, some people use .h for "header associated with a source file" and .hpp for "header with all functions defined inline".
Considering this, the main advantage would come in consistently naming your headers in the same style, and making that style readily apparent to anyone examining your code. This way, anyone familiar with your usual coding style will be able to determine what you mean with any given extension with just a cursory glance.
The extension of the source file may have meaning to your build system, for example, you might have a rule in your makefile for .cpp or .c files, or your compiler (e.g. Microsoft cl.exe) might compile the file as C or C++ depending on the extension.
Because you have to provide the whole filename to the #include directive, the header file extension is irrelevant. You can include a .c file in another source file if you like, because it's just a textual include. Your compiler might have an option to dump the preprocessed output which will make this clear (Microsoft: /P to preprocess to file, /E to preprocess to stdout, /EP to omit #line directives, /C to retain comments)
You might choose to use .hpp for files that are only relevant to the C++ environment, i.e. they use features that won't compile in C.