tags:

views:

2406

answers:

15

I have seen C++ code live in both .cc and .cpp files.

Which of these (or another!) is the best practice/most modern/best to use? http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml seems to suggest .cc, are there any other opinions or options?

I am mainly concerned with programs on Linux systems.

=:)

+46  A: 

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 onto the actual work.

JaredPar
+13  A: 

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

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

Ryu
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
@jd. Agreed. It makes automated tools a wee bit easier if h/c files turn into hpp/cpp files.
Paul Nathan
+6  A: 

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

Ben S
+1  A: 

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.

Kinopiko
+1  A: 

cpp = c plus plus and it thus my preferred, what does cc or cxx stand for?

Andrew
cxx is c++ after the plus signs fall over. They're unstable, you know, standing on one point like that. The c is more stable. This is also how the Apple II wound up as the Apple //.
David Thornley
@David - Maybe I'm just really old, but I remember Apple II as "Apple ][".
Fred Larson
@David Oh I never thought of that.
Andrew
I remember it being Apple ][ and the Apple //e. Who knows?
Joshua
A: 

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.

Dmitry
A: 

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.

FigBug
+4  A: 

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

Toji
+2  A: 

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
A: 

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

brone
But "cpp" could also stand for "C preprocessor". In fact, the program "cpp" on your system is most likely the C preprocessor...
Jesper
Maybe `.cc` stands for "see? see? it worked!"
Joshua
+3  A: 

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:

Aurélien Vallée
do you have an references to this? I've never seen OSS .cc vs Windows .cpp
bobby
A: 

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.

Ben Voigt
shouldn't that be .cwc then? :)
Joshua
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
A: 

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
+7  A: 

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 http://gcc.gnu.org/onlinedocs/gcc-4.4.1/gcc/Overall-Options.html#index-file-name-suffix-71

Clifford
+1  A: 

Found this post while searching for the answer to the same question as the poster. Great advise on which to use for the makefile and other tools! I didn't think about considering non-compiler tools while deciding on this and it was a great help.

While this might be a bit of necroposting, I thought it might still help others who find this also:

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 "C++ Primer Plus" book):

Unix uses: C, cc, cxx, c

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

Digital Mars uses: cpp, cxx

Borland C++ uses: cpp

Watcom uses: cpp

Microsoft Visual C++ uses: cpp, cxx, cc

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

So, I guess different environments support different extensions. Based on this article I think I might go with .hpp and .cpp for ease of cross-platform/cross-tool recognition.

John