Question

Is using .h as a header for a c++ file wrong?

I see it all over the place, especially with code written in the "C style". I noticed that Emacs always selects C highlighting style for a .h header, but c++ for hpp or hh.

Is it actually "wrong" to label your headers .h or is it just something which annoys me?

EDIT:

There is a good (ish) reason why this annoys me, if I have project files labelled, 'hpp & cpp' I can get away with 'grep something *pp' etc. otherwise I have to type '.h cpp'

Was it helpful?

Solution

It's not wrong to call your C++ headers .h. There are no rules of what extensions your code must use. For your non-headers, MSVC uses .cpp and on Linux, .cc as well. There is no one global standard, and .h is definitely very widely used.

But I'd say calling your headers .hpp (I've seen .hh a few times as well) is a lot more consistent and informative than just using .h.

OTHER TIPS

nothing wrong with that. This is the default with Microsoft Visual C++.

Just follow the standard you like, and stick with it.

You're free to use .h, .H, .hpp, .hxx, .hh, or whathaveyou. Just like you're free to use .c for C sources, and .C, .cpp, .cc, or .cxx for C++ sources. The extension is largely a convention.

It's like with the other files - .c for C, .cpp for C++ - use .h for C, .hpp for C++. This will satisfy most compilers/editors. It's not really wrong to not do so, but it can be used to give a hint which of the both languages you are actually using.

If you want to stop that behaviour without renaming the files, you should have a look at the Emacs config and try to change the source code detection.

Since Emacs has no easy way of finding out whether a .h file is C or C++, it's not ridiculous to choose C for .h files and C++ for .hpp files.

Note that all boost header files are suffixed with .hpp.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top