Header File extension .h or .hpp

I came across a few postings on another forum recently that debated which extension is correct and standard. From what I understand a .h header file is a C/C++ file while a .hpp is supposed to be a pure C++ file. I read that the Boost libraries (which I have yet to jump into) use .hpp extensions which is the future of the C++ spec. Is there any dominate preference out there or standard you see in place today? Advantages, disadvantages? I currently use .h.

Return 0;
The thing is, headers (any .h, .hpp, or any other extension I can't remember right now) are not compiled. They are only included by .cpps, so it doesn't really matter what extension they have, as long as the compiler knows it's not supposed to compile it.
.h is inherited from C.

Typically, C applications use .h and .c (or .cc)
C++ use .hpp and .cpp (pp = plus plus).

It comes down to where you work/study and the preference they have to naming conventions. You'll notice even compilers are moving away from the .h for C++ (e.g it's now #include <iostream> not <iostream.h>

And your right. Boost uses .hpp, but that's part of their convention.
Last edited on
Where I work, .h and .c are C files and .H and .C are C++.
And the compiler doesn't complain?
I don't think that a compiler cares much about the file extension, it just compiles what you tell it to be compiled
Not really.
GCC, for instance, ignores all headers even if you explicitly tell it to compile them.
The valid extensions (supported by GNU) for C++ source files are .cc, .cpp, .cxx, or .C. The extension of the header file doesn't matter because it is just inserted into the source file by the preprocessor.
If you're using C++ it's better practice to use .hpp because .h is from C and so is .c, just like you use .cpp in C++ you should use .hpp in C++, Won't make a difference though.
closed account (42hU7k9E)
Is iostream.h different than iostream? Why so?
iostream without .h is the new version, the extension was removed to distinguish the two versions.
Most compilers that are up to standard don't the .h anymore, they ignore all headers.
Last edited on
There are times you want to distinguish between C and C++ headers. If you are using just one language, it doesn't really matter what you call your header files. But if you mix the languages in a library or application, it becomes important to seperate them.

You can probably reply on __cplusplus being defined, and make up macros that wrap ' extern "C" ' and such C++ specific content, but the languages have subtle differences when confronted with common code (the size of enums, treatment of const variables for example).

Back in the old CFront days, there was a .C and .H convention for C++ source and header files to avoid conflict with .c and .h for C. However, MS-DOS used a case-insensitive file system and so used .cpp and .hpp instead. Visual Studio's parser still recongnises .hpp and .inl as C++ files, so it's not a completely forgotten form.

I think, if you're programming in just C++, feel free to use .h header files like everyone else. But if you're mixing languages, use .hpp for C++ and reserve .h for C.

I also think it was a mistake to start using .h for C++ programs.
Topic archived. No new replies allowed.