As a C++ developer, you would see .cpp for C++ source files and .h for header files. However, you may have seen file extensions like .cxx, .cc or .hxx. What are these file extensions less commonly? Why are they used and not others?

History

Before C++, there was the C language. C used the extensions .c for C source files and .h for C header files. Later, when C++ came about the file extensions .c and .h were still being used for C++. However, C++ code became incompatible with C code, thus using the same file extension would cause confusion between C++ and C languages. There was a need at the time to differentiate C++ code from C code.

C++ was developed on Unix to analyze the Unix kernel 1. Unix as an operating system has a case sensitive file system. Naturally file extensions .C and .H came about from case sensitivity, along with others like .c++, .cc, and .cxx. Similarly, for C++ header files, there was .H, .h++, .hh, .hxx, and .hpp.

However, other operating systems, Windows and DOS, did not have a case sensitive file system nor did DOS supported + in the file extension 2. Windows today is not case sensitive by default but can have ASCII (e.g., +) in the file name.

Do file extensions matter to the C++ Compiler?

No, file extensions do not matter to the C++ compiler. Thus, C++ source and header files only matter in the context of configuration of the file (i.e., is it a source file or a header file), organization, and IDE detection (i.e., what syntax to show in the editor).

CXX vs CPP

There is no difference between CXX and CPP, and the compiler doesn’t care. So, .cpp won the C++ source extension battle, right? Yes, mostly, but there is another extension that is popular with the Linux crowd: .cxx. The CXX convention used for Linux’s MAKE command. CXX makes itself be better used for Linux source files. However, this is not a requirement. CMAKE which runs on different operating systems including Windows, refers to C++ as CXX.

CC vs CPP

What is the difference between these two extensions? Again, no difference, they are all the same C++ source files to the compiler.

HPP vs H

Interestingly, the .h extension stayed with C++. Even though a popular convention, it has an issue that it is not clear that if the header will be C compatible. I’m not sure why .h stuck. If I speculate, the software developers at the time C++ could replace C altogether such C language did to B language. Again the C++ compiler does not care about the extension.

When to use .hpp?

Most of the time using .h is fine.

Here are reasons you could use .hpp over .h:

  • If the project will be distributed in a library then differentiating between .h and .hpp can help the developer integrating the library know that it is a C++ header.
  • If the project has both C++ and C source files then again it would best practice to differentiate by using .h for C and .hpp for C++.
  • If the project entire source is in one source file (no other .cpp files) then .hpp can be used.

Maximize Portability

Maximizing file system portability, and removing the non-portable file extensions gives us:

  • C++ source files: .cpp, .cxx, .cc
  • C++ header files: .hpp, .hh, .hxx

Today .c++ and .h++ can be used on modern operating systems but are rarely seen in the wild.

Over time, .cpp and .h became the popular choice with developers but have issues and there are other extensions.

Conclusion

There is no difference between CXX and CPP, they are exactly the same as the compiler does not care about file extensions. The different file extensions help configure and organize the files for the project for developer use.