What happened to the .h in C++ header files?

Posted on 07/05/2018 by Ken Gregg

In C++, the standard library headers are not necessarily implemented as files, and thus have no file extensions. The compiler implementation is free to implement headers however they wish. They might be files, they might be built into the compiler, they might be in a database, they might be files with completely different names, etc. That’s why they are referred to in C++ as headers, rather than as header files. In practice, many implementations just implement the standard C++ headers as files, but there’s no guarantee of this.

And if you mistakenly add a .h extension to a standard C++ header, you may be referring to something else entirely!

For example, the C++ <string> header is a completely different animal from the C <string.h> header file. In many C++ implementations, both could be included in the same C++ source file, but one gives you what you need for the C++ string class, while the other gives you access to the functions used to manipulate null-terminated C strings. In most cases, C header files that can be used in C++ have been provided in C++ with a ‘c’ prefix and without the .h extension (e.g., <cstring> is used to get access to null-terminated C string manipulation functions in C++).

Many implementations support the use of .h and other extensions (e.g., .hpp, .hxx, etc.) for use with your own header files. Check your compiler documentation for details on what specific header file extensions are supported in your environment.

header files headers file extensions project organization standard library software development software engineering programming the c++ programming language programming in c++ c++