159

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

From Google, I learned that they are both from the C++ language, but I am unsure of differences between them.

Bob Jones
  • 37
  • 2
  • 11
lv_laker
  • 1,625
  • 2
  • 11
  • 10

4 Answers4

280

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++.

tosh
  • 5,222
  • 2
  • 28
  • 34
James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • How can I configure Visual Studio to treat a file as C++ if it is not already recognised as such? – Avi Chapman Mar 05 '21 at 00:43
  • @AviChapman, you probably can try [files.associations](https://code.visualstudio.com/docs/languages/identifiers) – Xiang Mar 10 '21 at 16:17
24

There is no difference. They're exactly the same.

Alon Gubkin
  • 56,458
  • 54
  • 195
  • 288
10

Technically for the compiler there is no difference. However, some compilers and/or build systems will guess how to compile your files based on the extension and may or may not detect "cc" (or "cpp" but that is more rare I guess) as a c++ file.

Dentoid
  • 572
  • 3
  • 10
6

Actually it all depends on what you and your compiler prefer. There is no difference between them at all.

Bodhi
  • 314
  • 2
  • 7