.c vs .cc vs. .cpp vs .hpp vs .h vs .cxx

221,837

Solution 1

Historically, the first extensions used for C++ were .c and .h, exactly like for C. This caused practical problems, especially the .c which didn't allow build systems to easily differentiate C++ and C files.

Unix, on which C++ has been developed, has case sensitive file systems. So some used .C for C++ files. Other used .c++, .cc and .cxx. .C and .c++ have the problem that they aren't available on other file systems and their use quickly dropped. DOS and Windows C++ compilers tended to use .cpp, and some of them make the choice difficult, if not impossible, to configure. Portability consideration made that choice the most common, even outside MS-Windows.

Headers have used the corresponding .H, .h++, .hh, .hxx and .hpp. But unlike the main files, .h remains to this day a popular choice for C++ even with the disadvantage that it doesn't allow to know if the header can be included in C context or not. Standard headers now have no extension at all.

Additionally, some are using .ii, .ixx, .ipp, .inl for headers providing inline definitions and .txx, .tpp and .tpl for template definitions. Those are either included in the headers providing the definition, or manually in the contexts where they are needed.

Compilers and tools usually don't care about what extensions are used, but using an extension that they associate with C++ prevents the need to track out how to configure them so they correctly recognize the language used.

2017 edit: the experimental module support of Visual Studio recognize .ixx as a default extension for module interfaces, clang++ is recognizing .c++m, .cppm and .cxxm for the same purpose.

Solution 2

Those extensions aren't really new, they are old. :-)

When C++ was new, some people wanted to have a .c++ extension for the source files, but that didn't work on most file systems. So they tried something close to that, like .cxx, or .cpp instead.

Others thought about the language name, and "incrementing" .c to get .cc or even .C in some cases. Didn't catch on that much.

Some believed that if the source is .cpp, the headers ought to be .hpp to match. Moderately successful.

Solution 3

It really doesn't matter.
If you feed .c to a c++ compiler it will compile as cpp, .cc/.cxx is just an alternative to .cpp used by some compilers.

.hpp is an attempt to distinguish header files where there are significant c and c++ differences. A common usage is for the .hpp to have the necessary cpp wrappers or namespace and then include the .h in order to expose a c library to both c and c++.

Solution 4

I use ".hpp" for C++ headers and ".h" for C language headers. The ".hpp" reminds me that the file contains statements for the C++ language which are not valid for the C language, such as "class" declarations.

Solution 5

Talking about .hpp extension, I find it useful when people are supposed to know that this header file contains C++ an not C, like using namespaces or template etc, by the moment they see the files, so they won't try to feed it to a C compiler! And I also like to name header files which contain not only declarations but implementations as well, as .hpp files. like header files including template classes. Although that's just my opinion and of course it's not supposed to be right! :)

Share:
221,837

Related videos on Youtube

user541686
Author by

user541686

Updated on August 24, 2020

Comments

  • user541686
    user541686 over 3 years

    Possible Duplicates:
    *.h or *.hpp for your class definitions
    Correct C++ code file extension? .cc vs .cpp

    I used to think that it used to be that:

    • .h files are header files for C and C++, and usually only contain declarations.
    • .c files are C source code.
    • .cpp files are C++ source code (which can also be C source code).

    then files like .hpp, .cc, and .cxx came along, and I got totally confused... what's the difference(s) between those? When do you use the "new" ones?

    • Christoph
      Christoph about 13 years
      I prefer .cxx over .cpp for consistency with makefile conventions (see gnu.org/software/make/manual/html_node/Implicit-Variables.ht‌​ml )
    • jschultz410
      jschultz410 over 5 years
      I agree with Christoph. For the longest time I thought CPPFLAGS in Makefiles was for passing compiler flags to the C++ compiler (as opposed to the C compiler). CPPFLAGS actually passes flags to the C Pre-Processor, which means it will likely affect your C code too. Eventually, I learned that CXXFLAGS is the correct Makefile variable for passing flags just to the C++ compiler.
    • naught101
      naught101 about 5 years
      One of you should add that recommendation as an answer. Combined with @AProgrammer's answer, it makes for a pretty specific recommendation.
    • Jed
      Jed almost 5 years
      @Christoph Counterpoint: Make ships with implicit rules for .cc, .C, and .cpp, but not .cxx. gnu.org/software/make/manual/html_node/…
  • Daniel Gallagher
    Daniel Gallagher about 13 years
    I would also note that .cc is more common (but not "standard" by any means) on UNIX-like systems, where .cpp is more common on Windows systems. At least in my observations.
  • Martin Beckett
    Martin Beckett about 13 years
    I thought Sun's c++ compiler used .cc but I couldn't find a reference.
  • Adam Rosenfield
    Adam Rosenfield over 5 years
    A note about .ii: .i and .ii are the default extensions used by GCC, and likely other compilers too, for preprocessed C and C++ source respectively, not necessarily inline header definitions. If you ask GCC to preprocess a source with the -E switch, you get a .i or .ii file which can then later be fed into gcc -c to compile it.
  • Luke_
    Luke_ about 3 years
    In the first paragraph you mention .c++ twice
  • AProgrammer
    AProgrammer about 3 years
    @Luke_, There is a period ending the sentence between .cxx and .C.
  • Luke_
    Luke_ about 3 years
    @AProgrammer LOLOL im so sorry