HOWTO · C++
Difference Between .cc and .cpp File Extensions in C++
Learn how to choose between .cc and .cpp C++ source files, compile either suffix with GCC, and avoid build-configuration problems when renaming files.
For ordinary C++ source files, .cc and .cpp are naming conventions, not different C++ languages. Use the suffix already used by the project or its build tooling; in a new project, choose one and use it consistently. With GCC, both suffixes are recognized as C++ source inputs.
Compile Either Suffix With GCC
Save this program as either main.cc or main.cpp:
#include <iostream>
int main() { std::cout << "C++\n"; }
Compile and run each filename with g++:
g++ -std=c++23 main.cc -o cc.out && ./cc.out
g++ -std=c++23 main.cpp -o cpp.out && ./cpp.out
Both commands print:
C++
This result was verified with g++ (Ubuntu 15.2.0-16ubuntu1) 15.2.0 on Linux. GCC’s overall-options documentation lists both .cc and .cpp among C++ source suffixes. The example needs GCC/g++ with C++23 support; use the language standard required by your project if it differs.
What the Extension Changes
The suffix helps a compiler driver or build tool decide how to classify an input file. It does not make the code inside one file a different form of C++ from code inside the other. A .cc or .cpp file can contain a complete program, functions, or implementation code for declarations in a header; using headers or classes is not a requirement of either suffix.
The C++ standard does not mandate one filename suffix. Compiler drivers and tools define their own recognition rules, so an existing repository’s convention matters more than a general platform stereotype.
Choose .cc or .cpp for a Project
| Situation | Practical choice |
|---|---|
| The repository and build files already use one suffix | Follow that suffix. |
A project rule requires .cc |
Use .cc; for example, the Google C++ Style Guide specifies .cc for C++ source files. |
| A new project has no rule | Choose .cc or .cpp and apply it consistently. |
Neither suffix is inherently more portable just because it is associated with a particular operating system. Check the compiler, IDE, and build configuration that the project actually uses.
Do Not Use .c as an Ordinary C++ Naming Convention
The driver matters here. GCC’s general suffix table classifies .c as C input. However, the g++ driver documentation says that g++ treats .c, .h, and .i inputs as C++ unless -x overrides the language. On the verified host, this command compiled the same C++ program when it was named main.c:
g++ -std=c++23 main.c -o c.out && ./c.out
It printed C++. In contrast, gcc -std=c23 main.c -o gcc-c.out failed on that fixture because the C driver treated it as C and could not find the C++ header iostream.
That distinction is a reason not to name normal C++ sources .c: the result can depend on which driver a build invokes. When you must override suffix inference, GCC accepts -x c++ and the Clang CommandGuide documents -x for specifying the language of subsequent inputs; MSVC documents /Tp to force a named file to be treated as C++. Exact build-system settings can differ.
Update the Build When You Rename a File
Renaming main.cc to main.cpp can still fail even though the source code is unchanged. CMake, Make rules, IDE project files, filename globs, generated build inputs, and scripts may list the old name or match only one suffix. Update those references, then rebuild the project to verify that the renamed source is compiled.
For the next organizational step, see how to use multiple C++ code files.