cppforever.com

File Extensions in C++

Summary

C++ uses multiple file extensions for both header files and implementation files. The choice of extension influences clarity, tooling, and the ability to distinguish C from C++ code. Modern C++ projects commonly use .hpp and .cpp, but other extensions exist based on historical conventions and toolchains.


Header File Extensions

  1. .h — legacy and ambiguous

    • Historically from the C language.
    • Does not indicate whether the contents are C or C++.
    • Can conflict with C standard library headers.
  2. .hpp — recommended for modern C++

    • Explicitly signals C++ header code.
    • Well supported by IDEs, linters, and C++ parsers.
    • Symmetric counterpart to .cpp.
  3. .hh — minimalist C++ style

    • Common in some codebases (often paired with .cc).
    • Short and clean, but less universally recognized.
  4. Module-related extensions

    • .ixx, .mxx, .cppm when working with C++20 Modules.
    • Not relevant unless using modular compilation.

Implementation File Extensions

The file extension depends on the implementation and on compiler or project conventions. Common extensions for C++ implementation files include:

*.cpp is the most widely recognized in modern C++ projects.


Practical Recommendation

For modern, long-term C++ codebases:

This yields a clear and idiomatic file structure and reduces ambiguity between C and C++ sources.


Related Notes