What is the difference between files with .h and .hpp in C ++ ? What is better to use?
2 answers
Strictly speaking, there is no difference between them at all. The difference can only be in how the IDE interprets them. .h , if you follow some logic, this is the header for C ( .c ), whereas .hpp is the header for C ++ ( .cpp , .cxx etc.). But all these are conventions and depend on preferences and IDEs used. For example, in Visual Studio we have .h and .cpp files, by default.
The only thing, in my opinion, where different naming can be useful, is in a project that combines C and C ++ code. Then you should have .h / .c for C-code and .hpp / .cpp for C ++ when. I see no other uses besides personal presentations.
- Why, then, when trying to specify in the
.hppfile, what should the class method do, for example:void test::test(){ ... }, g ++ did not see this point-blank? - user26699 - @egordorichev,
hppis a header, you are apparently trying to use it as a code file. - ixSci - but in
.h, such a stunt rolls, although it is not quite good. - user26699 - Well, in general, this is not a trick, this is a normal situation. I just decided that you have a
.hfile with an interface that you implement in the.hppfile. It's hard for me to judge gcc, because I do not use it and do not know what he does not like. - ixSci
There is no principal difference in the behavior of the preprocessor depending on the header expansion. For the most part, he doesn’t care about the extension ; he will include a file with or without any extension.
Different extensions for C and C ++ headers are made more for the convenience of programmers. If you see a .hpp header, don’t even try to include it in a project written in pure C, because most likely it will describe structures specific only to C ++ (classes, templates, etc.). Header .h can (with an eye to the fact that C is not a subset of C ++) to include in projects written in C ++. The IDE can also apply different formatting and syntax highlighting rules for .h and .hpp headers.