C++ Code File Extension? Difference Between .Cc and .Cpp

What is the difference between .cc and .cpp file suffix?

Conventions.

Historically, the suffix for a C++ source file was .C.
This caused a few problems the first time C++ was ported
to a system where case wasn't significant in the filename.

Different users adopted different solutions: .cc,
.cpp, .cxx and possibly others. Today, outside of the Unix
world, it's mostly .cpp. Unix seems to use .cc more often.

For headers, the situation is even more confusing: for whatever
reasons, the earliest C++ authors decided not to distinguish
between headers for C and for C++, and used .h.

This doesn't cause any problems if there is no C in the project, but when you
start having to deal with both, it's usually a good idea to
distinguish between the headers which can be used in C (.h)
and those which cannot (.hh or .hpp).

In addition, in C++, a lot of users (including myself) prefer keeping the template
sources and the inline functions in a separate file. Which,
while strictly speaking a header file, tends to get yet another
set of conventions (.inl, .tcc and probably a lot of
others).

In the case of headers it makes absolutely no difference to the compiler.

In the case of source files different endings will cause the compiler to assume a different
language. But this can normally be overridden, and I used .cc
with VC++ long before VC++ recognized it as C++.

C++ code file extension? What is the difference between .cc and .cpp

At the end of the day it doesn't matter because C++ compilers can deal with the files in either format. If it's a real issue within your team, flip a coin and move on to the actual work.

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

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.

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

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.

Is there any reason Google uses the cc extension instead of cpp in their open source projects?

It's entirely a matter of personal preference, at least for the person(s) starting the project. Whatever you choose, be consistent.

Correct C++ file extension

It's a matter of code conventions. These extensions are all interchangeable (as far as platforms are concerned). If you participate in an existing project, you should adhere to this project's conventions, otherwise just choose whatever you like.

Why does the filename extension make a difference to compiling?

C++ is going to error on your implicit cast from the void* returned by malloc() to int*. Whereas C allows implicit casts from void* to other pointer types.

Most compilers will default to looking at the file extension to determine language to compile to.

A man gcc reveals that all .c files default to being compiled as C. Whereas all .cc, .cp, .cxx, .cpp, .CPP, .c++, and .C (capital C) files are compiled as C++.

You can override this behavior force the language via the -x option for gcc/g++.

Example:

 gcc -x c++ foo.c -c   // compiles foo.c as C++ instead of C

gcc and g++ are typically the same binary on most unix systems. It just defaults to different behavior depending on its own argv[0] parameter.

There might be other behavior differences between explicitly using g++ and gcc versus the -x option. I'm not certain on that.

C++ code file extension? What is the difference between .cc and .cpp

At the end of the day it doesn't matter because C++ compilers can deal with the files in either format. If it's a real issue within your team, flip a coin and move on to the actual work.

Why some projects choose the extension for source files .cc in c++?

C++ is the ultimate language of choice and flexibility and C++ developers like to be different. The .cc extension is just one of the many that people choose for header and source files. Some others I've seen.

  • No extension: Popular with header files
  • .h
  • .hpp
  • .cpp
  • .cc
  • .c
  • .C (explicit capital on case sensitive file systems)
  • .cxx
  • .inl (for inline templates)

Which to use is merely a matter of preference. There is no inherent gain from choosing one extension over the other.

The only real effect the extension has is to kill a team's productivity for a day or two while they debate the best one to use.



Related Topics



Leave a reply



Submit