Question
I have seen C++ source files saved with both .cc and .cpp extensions.
Which one is considered best practice, more modern, or generally preferred to use? The Google C++ Style Guide appears to suggest .cc, but are there other common opinions or alternatives?
I am mainly concerned with C++ programs on Linux systems.
Short Answer
By the end of this page, you will understand what C++ file extensions like .cc and .cpp mean, why both are valid, and how teams usually choose between them. You will also learn the practical rule that matters most: consistency within a project is more important than the specific extension you pick.
Concept
In C++, file extensions such as .cc, .cpp, .cxx, and sometimes .C are mostly conventions used to indicate that a file contains C++ source code.
They do not change the C++ language itself. What matters is how your compiler, build tool, IDE, and team treat those files.
The key idea
A C++ compiler needs to know that a file should be compiled as C++. File extensions are one common way tools infer that.
Common source file extensions include:
.cpp.cc.cxx.Con some systems
Common header file extensions include:
.h.hpp.hh.hxx
Is there a technical difference between .cc and .cpp?
Usually, no meaningful language difference exists between .cc and .cpp.
Both are commonly recognized by compilers and build systems as C++ source files. The difference is mostly:
Mental Model
Think of .cc and .cpp like two different labels on the same kind of storage box.
- One box is labeled
books - Another box is labeled
reading-material
The labels look different, but both boxes can contain books.
Similarly:
.ccmeans “this is C++ source code”.cppalso means “this is C++ source code”
The important part is that everyone in the room agrees on the labeling system. In a programming team, that means using one convention consistently so developers and tools know what to expect.
Syntax and Examples
C++ file extensions are not syntax in the language itself, but they are part of how source files are organized.
Common file naming patterns
main.cpp
math_utils.cpp
parser.cc
server.cc
vector_helpers.cxx
Example with .cpp
// main.cpp
#include <iostream>
int main() {
std::cout << "Hello, C++\n";
return 0;
}
Compile it on Linux with:
g++ main.cpp -o app
Example with .cc
// main.cc
#include <iostream>
int main() {
std::cout << "Hello, C++\n";
return 0;
}
Compile it the same way:
Step by Step Execution
Consider this file:
// greet.cpp
#include <iostream>
int main() {
std::cout << "Hi\n";
return 0;
}
Now walk through what happens when you compile it:
g++ greet.cpp -o greet
Step by step
- The compiler sees the filename
greet.cpp. - The
.cppextension helps the tool recognize it as a C++ source file. - The compiler parses the file using C++ rules.
#include <iostream>brings in declarations needed for output.- The
mainfunction is compiled. std::cout << "Hi\n";becomes machine instructions for printing text.- The executable
greetis created.
If the file were named greet.cc instead:
g++ greet.cc -o greet
The same general process happens. The extension is different, but the compiler still treats it as C++ source in normal setups.
Real World Use Cases
Here are common situations where file extension conventions matter.
1. Team-based application development
A backend service may have dozens or hundreds of C++ files. Teams choose .cc or .cpp so the whole repository looks uniform.
2. Build systems
Tools like Make, CMake, Bazel, or Meson often detect source files by extension. Using standard extensions helps these tools work smoothly.
3. IDE and editor support
Editors use file extensions to decide:
- syntax highlighting
- code completion
- linting
- formatting rules
4. Cross-platform projects
A project built on Linux, macOS, and Windows may choose .cpp because many developers immediately recognize it. Another project may choose .cc because that is the team standard.
5. Large organizations
Large companies often standardize naming conventions through style guides. This makes automation and review easier.
Real Codebase Usage
In real projects, developers rarely debate file extensions in isolation. They usually choose a convention as part of a broader codebase style.
Common patterns
1. Follow the existing repository
If a codebase already uses .cc, new files should usually also use .cc.
If it uses .cpp, stick with .cpp.
This is similar to other style choices like:
- brace formatting
- naming conventions
- header extension choice
- test file naming
2. Build tool configuration
A build file may explicitly list sources:
add_executable(app
main.cpp
parser.cpp
config.cpp
)
Or:
add_executable(app
main.cc
parser.cc
config.cc
)
Both are fine as long as the files exist and the project is consistent.
3. Header/source pairing
Teams often choose matching patterns to make files easier to find:
widget.cppwithwidget.hppwidget.ccwith
Common Mistakes
1. Thinking .cc and .cpp mean different C++ versions
They do not indicate different language standards like C++11, C++17, or C++20.
The language version is chosen with compiler flags, for example:
g++ -std=c++20 main.cpp -o app
2. Mixing conventions randomly
Broken example from a style perspective:
main.cpp
parser.cc
math_utils.cxx
engine.cpp
This may still compile, but it looks inconsistent and can confuse tooling or teammates.
3. Assuming every tool treats every extension the same
Most modern tools handle common C++ extensions, but not every script or custom build setup does.
If a tool only searches for *.cpp, then .cc files may be skipped.
4. Using unusual or ambiguous extensions without checking tools
Some extensions are less common and may need extra configuration.
5. Confusing .c with C++ source
Broken example:
// main.c
#include <iostream>
int {
:: << ;
}
Comparisons
| Extension | Common meaning | Notes |
|---|---|---|
.cpp | C++ source file | Very common, especially easy to recognize |
.cc | C++ source file | Common on Linux and in some style-guided codebases |
.cxx | C++ source file | Less common, but still valid in many toolchains |
.C | C++ source file | Historically used in some environments; can be problematic on case-insensitive filesystems |
.c | C source file | Usually treated as C, not C++ |
.cc vs .cpp
Cheat Sheet
Quick reference
.ccand.cppboth usually mean C++ source file.- There is usually no language-level difference between them.
- On Linux, both are commonly used.
- The best practice is usually follow the existing project convention.
- If starting a new project, pick one and use it consistently.
Common source extensions
.cpp
.cc
.cxx
Common header extensions
.h
.hpp
.hh
.hxx
Compile examples
g++ main.cpp -o app
g++ main.cc -o app
Important rule
File extension does not choose the C++ standard.
Use compiler flags for that:
g++ -std=c++17 main.cpp -o app
g++ -std=c++20 main.cc -o app
Best practice
- existing codebase > personal preference
- consistency > extension debate
- standard tool support > unusual naming
FAQ
Is .cc better than .cpp for C++ on Linux?
No. Both are commonly used on Linux. The better choice is usually whichever convention your project already follows.
Do .cc and .cpp compile differently?
Usually no. Standard C++ compilers generally treat both as C++ source files.
Which extension is more modern in C++?
Neither is inherently more modern. They are naming conventions, not language features.
What extension should I use for a new C++ project?
Pick one standard extension such as .cpp or .cc, then use it consistently across the project.
Why do some style guides prefer .cc?
Style guides often choose one convention to keep large codebases consistent. That does not make other common extensions wrong.
Can I mix .cc and .cpp in one project?
You can, but it is usually not recommended unless there is a specific reason. Consistency makes projects easier to maintain.
Does the file extension control whether I use C++11 or C++20?
No. The C++ standard version is controlled by compiler options like -std=c++20.
Mini Project
Description
Create a tiny C++ project on Linux to see that .cc and .cpp are both valid source file extensions. This project helps you practice consistent file naming and basic compilation with multiple source files.
Goal
Build and run a small two-file C++ program, then rename the source files between .cpp and .cc to observe that the code still works when the build command matches the filenames.
Requirements
Create a C++ program with at least two source files and one header file.
Use either .cpp or .cc consistently for the source files.
Compile the program with g++ on Linux.
Rename the source files to the other extension and update the compile command.
Verify that the program still builds and runs correctly.
Keep learning
Related questions
Advantages of Brace Initialization in C++
Learn why C++ brace initialization is often clearer and safer than other object initialization styles, with examples and common pitfalls.
Basic Rules and Idioms for Operator Overloading in C++
Learn the core rules, syntax, and common idioms for operator overloading in C++, including member vs non-member operators.
C++ Aggregates, Trivial Types, Trivially Copyable Types, and PODs Explained
Learn what aggregates, trivial types, trivially copyable types, and PODs mean in C++, how they differ, and why they matter.