C++ code file extension? .cc vs .cpp

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.


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.


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


Great advise 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 "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++

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.

链接地址: http://www.djcxy.com/p/37132.html

上一篇: C ++:模板不能从另一个类运行

下一篇: C ++代码文件扩展名? .cc vs .cpp