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