750

I have seen C++ code saved as both .cc and .cpp files. Is there a difference between the two?

The Google style guide seems to suggest .cc, but provides no explanation.

I am mainly concerned with programs on Linux systems.

Klesun
  • 12,280
  • 5
  • 59
  • 52
Jessica
  • 1,421
  • 5
  • 15
  • 10
  • 137
    **Conclusion** It doesn't matter. **Possible Origin** cc = C with classes, cpp = C plus plus – Lazer Sep 28 '10 at 18:42
  • 11
    It matters to clang++. When you give it a C++ header file with a name that ends in .h, clang++ warns you. – allyourcode Apr 13 '13 at 08:37
  • 7
    Another tool that cares a little is emacs. With a clean .emacs config, opening ("finding" in emacs parlance) a .h file activates c-mode, not c++-mode. Of course, you can configure emacs to do something else (as with everything in emacs), but my point is that c-mode is the out-of-the-box default. – allyourcode Apr 13 '13 at 08:51
  • 8
    `lint` cares, `.C` is C++ and `.c` is C with no understanding whatsoever of `.cc` or `.cpp`. At least on AIX 6.1. – Jesse Chisholm Nov 06 '15 at 00:16
  • 20
    Answering "it doesn't matter" doesn't really help. The question is totally relevant. The OP was looking for a solid convention to stick to. A better answer would be: "Unfortunately, the C++ community does not have a solid convention on this". It's sad, if you think about it. All other popular languages seem to have a single, unique file extension. I would stick to what an important project uses, like gcc. [They use `.cc`](https://github.com/gcc-mirror/gcc/search?l=cpp). – Lucio Paiva Sep 24 '16 at 11:41
  • 4
    **.cc** is very confusing as `cc` is a very common name for the `C` compiler. – Galik Nov 09 '17 at 12:08
  • also .cxx in some implementations. – mckenzm Aug 20 '19 at 06:23
  • 1
    I dislike the CPP acronym as it conflicts with the C Pre-Processor. – andypea Nov 25 '19 at 06:19
  • 1
    Bjarne Stroustrup whom designed the C++, prefered CPP in his very famous book: "The C++ Programming Language" from page 67 and so on. – MateusR Jul 19 '21 at 03:38
  • Answering "it doesn't matter" is the most helpful possible way to address the question. The question is irrelevant, because it **really doesn't** matter. The purpose of file extensions is to indicate the content of the file (i.e. to serve as metadata), but that metadata **isn't needed** by the compiler - it's only something used by programmers for organizational and documentation purposes, and other programmers **will understand you either way**. Other popular languages often **don't** use a file extension at all (e.g. Python files intended to be run as scripts, especially on Linux). – Karl Knechtel Feb 10 '23 at 20:57
  • .cc not recognized by some toolchains at all. Or misdirected, because it was traditionally used by Embrocadero\Builder\Delphi (and their flavor of "GUI C++") – Swift - Friday Pie Feb 17 '23 at 16:13

17 Answers17

893

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.

Emerick Rogul
  • 6,706
  • 3
  • 32
  • 39
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 157
    Well, that is a valid point, but it does not answer the user's question. – vikrantt May 11 '15 at 20:04
  • 470
    cc is faster to type – thang Aug 07 '15 at 15:58
  • 101
    Why is this the accepted answer? A newer programmer will not know that it does not matter and they deserve a straightforward answer. – Robben_Ford_Fan_boy Jul 19 '16 at 15:37
  • 40
    The answer is absolutely not enough for a curious and mindful programmer. I'd like the answer on [this page](https://stackoverflow.com/questions/18590135/what-is-the-difference-between-cc-and-cpp-file-suffix) more since it has more detailed explanations. – Novin Shahroudi Oct 25 '17 at 07:26
  • 4
    When one considers that the compiler is not typically the only tool involved -- almost always, there is "make" or a similar utility that WILL care which extensions you use, for build rules matching -- then this answer really does not address the core concerns of the question. Note that there are variations by system and by toolchain (including your favorite make rules etc) which will have an impact on the decision. For instance, the default recursive multi-target build system some of in the QNX 6 series of development platforms don't pick up *.cpp files as C++ language sources; it wantss .cc – JoGusto Apr 15 '19 at 18:55
  • @thang `C` may very well be even faster than `cc` – phuclv Jun 08 '19 at 10:39
  • 3
    Filename extension != file format. – Junuxx Sep 10 '20 at 20:19
  • @Robben_Ford_Fan_boy it is the accepted answer because it is a straightforward answer that correctly explains the truth, that it does not matter, which is a truth that is the same regardless of the programmer's level of experience. – Karl Knechtel Feb 10 '23 at 20:58
370

GNU GCC recognises all of the following as C++ files, and will use C++ compilation regardless of whether you invoke it through gcc or g++: .C, .cc, .cpp, .CPP, .c++, .cp, or .cxx.

Note the .C - case matters in GCC, .c is a C file whereas .C is a C++ file (if you let the compiler decide what it is compiling that is).

GCC also supports other suffixes to indicate special handling, for example a .ii file will be compiled as C++, but not pre-processed (intended for separately pre-processed code). All the recognised suffixes are detailed at gcc.gnu.org

Ziezi
  • 6,375
  • 3
  • 39
  • 49
Clifford
  • 88,407
  • 13
  • 85
  • 165
  • 8
    "case matters in GCC" - What about Windows (since it is case-insensitive) ? – Devesh Khandelwal Sep 13 '15 at 09:32
  • 23
    @Devesh : Windows too. But the OS will prevent you from having two files in a folder distinguished only by case. – Clifford Sep 13 '15 at 11:51
  • 30
    @DeveshKhandelwal But it is case-preserving – Yatharth Agarwal Dec 02 '15 at 21:24
  • @Clifford Theoretically all versions of NT _will_ allow you to have two (or more) files in a folder distinguished only by case, provided you pass `FILE_FLAG_POSIX_SEMANTICS` or similar in the appropriate places; that’s how the original POSIX subsystem, Interix/SFU/SUA and even WSL 1 worked. XP and later require you to [jump through some hoops](https://blog.steamsprocket.org.uk/2010/02/26/posix-file-semantics-in-windows) to enable this, however, and many, many applications will break if you do it (including AV scanners, thus the hoops). – Alex Shpilkin Aug 05 '21 at 06:03
  • 1
    @AlexShpilkin Interesting, tough not particularly relevant in this context. If you did that and had such files, your project could not be checked out and built on PC, without this configuration. A build that depends on the filesystem config would be a bad idea. – Clifford Aug 05 '21 at 06:09
330

Great advice on which to use for the makefile and other tools, considering non-compiler tools while deciding on which extension to use is a great approach to help find an answer that works for you.

I just wanted to add the following to help with some .cc vs .cpp info that I found. The following are extensions broken down by different environments (from the "C++ Primer Plus" book):

Unix uses: .C, .cc, .cxx, .c

GNU C++ uses: .C, .cc, .cxx, .cpp, .c++

Clang uses: .C, .cc, .cxx, .cpp, .c++ and also .cppm for module interfaces

Digital Mars uses: .cpp, .cxx

Borland C++ uses: .cpp

Watcom uses: .cpp

Microsoft Visual C++ uses: .cpp, .cxx, .cc and also .ixx for module interfaces

Metrowerks CodeWarrior uses: .cpp, .cp, .cc, .cxx, .c++

The different environments support different extensions. I too was looking to answer this question and found this post. Based on this post I think I might go with .hpp and .cpp for ease of cross-platform/cross-tool recognition.

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
John S.
  • 1,937
  • 2
  • 18
  • 28
92

.cpp is the recommended extension for C++ as far as I know. Some people even recommend using .hpp for C++ headers, just to differentiate from C.

Although the compiler doesn't care what you do, it's personal preference.

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Ryu
  • 8,641
  • 10
  • 65
  • 98
  • 78
    I decided to switch from using .h to using .hpp for c++ headers; primarily because other tools like editors need to know as well - In addition when using precompiled headers with gcc, it defaults to using C for .h files and C++ for .hpp files unless you use the '-x c++-header' option when precompiling a .h file. – jdkoftinoff Oct 09 '09 at 18:00
  • 7
    @jd. Agreed. It makes automated tools a wee bit easier if h/c files turn into hpp/cpp files. – Paul Nathan Oct 09 '09 at 20:34
  • 4
    g++ doesn't recognize .hpp as a C++ header (for header precompilation) but .hh it does. Because of this I ended up using .cc/.hh over .cpp/.hpp as there really isn't any real difference. – Tronic Dec 09 '10 at 18:41
  • 15
    @Tronic: since 4.3 gcc recognizes .hpp, compare http://gcc.gnu.org/onlinedocs/gcc-4.2.4/gcc/Overall-Options.html with http://gcc.gnu.org/onlinedocs/gcc-4.3.6/gcc/Overall-Options.html – CesarB Aug 26 '11 at 20:50
  • 1
    @jdkoftinoff Actually, for a very long time people have been capitalizing the H in the header extension to denote C++ (prior to .hpp). Now that you can use .hpp, go for it. Don't be surprised though if you see some headers like so: Header.H and Header.h <- The first is cpp, the latter is c [if the application uses C and C++ you will see this a lot] – Charles D Pantoga Sep 28 '13 at 15:42
  • 3
    @CharlesAddis - Yes, I had to convert lots of code that had "abcd.H" (the c++ interface) and "abcd.h" (the C interface) in the same directory to "abcd.hpp" and "abcd.h" because just doing an "svn co" or unzip onto a Windows box or Mac OS X box (with default filesystem) would fail due to the "duplicate file names" – jdkoftinoff Sep 28 '13 at 22:23
  • 1
    @jdkoftinoff: Yeah, having two files whose names differ only in case is stupid. OTOH, why do you have `abcd.hpp` _and_ `abcd.h` in the first place? They should not both be called `abcd`. – Lightness Races in Orbit Mar 14 '15 at 19:06
  • Not my code! Found in other code such as this example of xt_HL.c and xt_hl.c in the same directory in linux: https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/xt_HL.c?id=refs/tags/v4.0-rc3 and https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/xt_hl.c?id=refs/tags/v4.0-rc3 – jdkoftinoff Mar 14 '15 at 21:18
44

I personally use .cc extension for implementation files, .hh for headers, and .inl for inline/templates.

As said before, it is mainly a matter of taste.

From what I've seen, .cc seems to be more "open source projects oriented", as it is advised in some great open source software coding styles, whereas .cpp seems to be more Windowish.

--- EDIT

As mentioned, this is "from what i've seen", it may be wrong. It's just that all Windows projects I've worked on used .cpp, and a lot of open source projects (which are mainly on unix-likes) use .cc.

Examples coding styles using .cc:

NewbiZ
  • 2,395
  • 2
  • 26
  • 40
  • 1
    do you have an references to this? I've never seen OSS .cc vs Windows .cpp – bobby Oct 09 '09 at 20:02
  • 7
    Visual Studio creates .cpp files for C++. I don't know the history behind it. – Natan Yellin Jun 13 '11 at 13:00
  • 9
    LLVM Coding Standard seems to advocate for .cpp/.h and putting `-*- C++ -*-` tag in the headers http://llvm.org/docs/CodingStandards.html ; Mozilla Coding Style suggests .cpp/.h https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Coding_Style ; KDE seems to be using .cpp/.h too http://quickgit.kde.org/ – sastanin May 17 '15 at 22:45
  • 1
    The 'Google coding style' link shown above is dead now. Try this Google style guide instead: https://google.github.io/styleguide/cppguide.html. Ditto for the KDE coding style in @sastanin's comment. For KDE coding style guides, try an Internet search using 'KDE coding style'. – Jim Fischer Aug 03 '20 at 10:59
22

Other file extensions used include .cxx and .C (capital C). I believe Bjarne Stroustrup used .C originally. .cpp is the name of the C preprocessor so it's unfortunate that it was used for C++ as well.

Ziezi
  • 6,375
  • 3
  • 39
  • 49
20

The other option is .cxx where the x is supposed to be a plus rotated 45°.

Windows, Mac and Linux all support .c++ so we should just use that.

Ziezi
  • 6,375
  • 3
  • 39
  • 49
Roland Rabien
  • 8,750
  • 7
  • 50
  • 67
17

Several people saying .cc doesn't stand for anything? It might. C++ started life as "C with Classes".

True that .cc and .cpp are also command names on most Unix systems (c compiler and c preprocessor respectively).

I use .cpp exclusively, but I started on Windows. .cc is more a Unix convention, although I see it less and less even there. GNU make has rules for .cpp so that's probably preferred, it will work by default on both Windows and everything else. On the other hand modern C++ uses no extension at all for headers, I really don't like that. All my projects use .h for header files, and they support both C and C++ as much as possible via extern "C" and testing __cplusplus.

Ziezi
  • 6,375
  • 3
  • 39
  • 49
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • 3
    shouldn't that be .cwc then? :) – Joshua Oct 09 '09 at 20:06
  • Before many compilers supported namespaces, they also used .h extension for standard headers. Commonly, compilers provide deprecated .h versions which place the library into the global namespace. This allows the support of legacy code. I read somewhere once that the reason they do not have .h extensions is that the standard allows them to be not files, but essentially 'built-in'. However that may be apocryphal. – Clifford Oct 09 '09 at 21:38
14

Just follow the convention being used for by project/team.

Ben S
  • 68,394
  • 30
  • 171
  • 212
11

I've personally never seen .cc in any project that I've worked on, but in all technicality the compiler won't care.

Who will care is the developers working on your source, so my rule of thumb is to go with what your team is comfortable with. If your "team" is the open source community, go with something very common, of which .cpp seems to be the favourite.

Ziezi
  • 6,375
  • 3
  • 39
  • 49
Toji
  • 33,927
  • 22
  • 105
  • 115
  • Several well-known projects such as https://github.com/google/googletest are using `.cc` as a file extension für C++ implementation files – Vertexwahn Jun 07 '19 at 14:20
11

The .cc extension is necessary for using implicit rules within makefiles. Look through these links to get a better understanding of makefiles, but look mainly the second one, as it clearly says the usefulness of the .cc extension:

ftp://ftp.gnu.org/old-gnu/Manuals/make-3.79.1/html_chapter/make_2.html

https://ftp.gnu.org/old-gnu/Manuals/make-3.79.1/html_chapter/make_10.html

I just learned of this now.

Kyle
  • 1
  • 1
  • 2
  • Again they are just `.cpp` files. No worries ! :-) – Tom Taylor Dec 05 '17 at 20:15
  • 2
    It says "We encourage you to use the suffix '.cc' for C++ source files instead of '.C'." I suspect that's just poor wording. Using `.C` can be problematic on systems with case-insensitive file systems. I don't think there's any particular advantage, as far as `make` is concerned, in using `.cc` over `.cpp`, for example. Makefiles work just fine with `.cpp` for C++ source files. – Keith Thompson Jan 19 '18 at 03:28
10

As with most style conventions, there are only two things that matter:

  1. Be consistent in what you use, wherever possible.
  2. Don't design anything that depends on a specific choice being used.

Those may seem to contradict, but they each have value for their own reasons.

Alan
  • 4,897
  • 2
  • 24
  • 17
8

It doesn't matter which of those extensions you'd use. Pick whichever you like more, just be consistent with naming. The only exception I'm aware of with this naming convention is that I couldn't make WinDDK (or is it WDK now?) to compile .cc files. On Linux though that's hardly a problem.

Ziezi
  • 6,375
  • 3
  • 39
  • 49
Dmitry
  • 6,590
  • 2
  • 26
  • 19
8

.C and .cc seem to be standard for the (few) Unix-oriented C++ programs I've seen. I've always used .cpp myself, since I only really work on Windows and that's been the standard there since like forever.

I recommend .cpp personally, because... it stands for "C Plus Plus". It is of course vitally important that file extensions are acronyms, but should this rationale prove insufficiently compelling other important things are non-use of the shift key (which rules out .C and .c++) and avoidance of regular expression metacharacters where possible (which rules out .c++ -- unfortunately you can't really avoid the . of course.).

This doesn't rule out .cc, so even though it doesn't really stand for anything (or does it?) it is probably a good choice for Linux-oriented code.

  • 5
    But "cpp" could also stand for "C preprocessor". In fact, the program "cpp" on your system is most likely the C preprocessor... – Jesper Oct 09 '09 at 20:03
8

I've use .C and .h for source and header, respectively. One nice thing with that choice is that, on the command line, its easy to use *.[Ch] to select all of the code files. Using .C could be a problem on case insensitive filesystems, but if you have foo.c and foo.C in the same directory, you deserve what you get anyway :)

KeithB
  • 16,577
  • 3
  • 41
  • 45
7

I am starting a new C++ project and started looking for the latest in C++ style. I ended up here regarding file naming and I thought that I would share how I came up with my choice. Here goes:

Stroustrup sees this more as a business consideration than a technical one.

Following his advice, let's check what the toolchains expect.

For UNIX/Linux, you may interpret the following default GNU make rules as favoring the .cc filename suffix, as .cpp and .C rules are just aliases:

$ make -p | egrep COMPILE[^=]+=
COMPILE.cc = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
COMPILE.cpp = $(COMPILE.cc)
COMPILE.C = $(COMPILE.cc)

(Note: there is no default COMPILE.cxx alias)

So if you are targeting UNIX/Linux, both .cc and .cpp are very good options.

When targeting Windows, you are looking for trouble with .C, as its file system is case-insensitive. And it may be important for you to note that Visual Studio favors the .cpp suffix

When targeting macOS, note that Xcode prefers .cpp/.hpp (just checked on Xcode 10.1). You can always change the header template to use .h.

For what it is worth, you can also base your decision on the code bases that you like. Google uses .cc and LLVM libc++ uses .cpp, for instance.

What about header files? They are compiled in the context of a C or C++ file, so there is no compiler or build system need to distinguish .h from .hpp. Syntax highlighting and automatic indentation by your editor/IDE can be an issue, however, but this is fixed by associating all .h files to a C++ mode. As an example, my emacs config on Linux loads all .h files in C++ mode and it edits C headers just fine. Beyond that, when mixing C and C++, you can follow this advice.

My personal conclusion: .cpp/.h is the path of least resistance.

Armorix
  • 41
  • 2
  • 2
7

As others wrote before me, at the end its what being used by your project/team/company.

Personally, I am not using cc extension, I am trying to lower the number of extensions and not increase them, unless there's a clear value (in my opinion).

For what its worth, this is what I'm using:

c - Pure C code only, no classes or structs with methods.

cpp - C++ code

hpp - Headers only code. Implementations are in the headers (like template classes)

h - header files for both C/C++. I agree another distinction can be made, but as I wrote, I am trying to lower the number of extensions for simplicity. At least from the C++ projects I've worked in, h files for pure-C are more rare, therefore I did not want to add another extension.

TCS
  • 5,790
  • 5
  • 54
  • 86