.cpp vs .cxx: what is the difference between C++ file extensions?
.cpp, .cxx and .cc all contain C++ source code. The extension is a naming convention, not a different language; here is where the variants come from and what to pick.
Created with the help of AI. This article has not been reviewed editorially yet. How we use AI and review content
You open a project and find main.cpp next to util.cxx and helper.cc. Are they different languages? Do they compile differently? Short answer: no. All three are C++ source files, and the extension is mostly a naming convention.
TL;DR
.cpp,.cxx,.cc,.c++and.Care all C++ source files; the code inside is identical in syntax and meaning.- Which extension a project uses is historical and a matter of style.
.cppis the most common default. - Compilers use the extension only to decide that a file is C++ (and not C). GCC, Clang and Microsoft's compiler all recognise
.cppand.cxx. - You can rename
.cxxto.cppwithout changing the code, but you must update build files that list the old name.
Why are there several extensions?
When C++ appeared, there was no agreed standard for its file names, and different tools picked different ones:
.C(capital C) was used on Unix, where.cmeant C and.Cmeant C++. On case-insensitive file systems such as Windows and macOS the two names collide, so other suffixes were needed..ccis common on Unix and in some large code bases..cpp(short for "C plus plus") became the default on Windows tools and in many IDEs..cxxuses the "x" as a rotated plus sign and is used by some projects, for example the Visualization Toolkit (VTK).
Header files show the same pattern: .h, .hpp, .hxx and .hh are all used for C++ headers.
How do compilers treat them?
The compiler does not care about the spelling beyond recognising the language. The GCC manual lists .cc, .cp, .cxx, .cpp, .c++ and .C as C++ source, and notes that in .cxx the last two letters must literally be an "x". Clang behaves the same way, and Microsoft's compiler treats .cpp, .cxx and .cc as C++ while compiling .c as C by default.
Build systems such as CMake, Make and Visual Studio projects list the source files explicitly or by pattern, so a .cxx file only needs to be included in that list.
Which extension should you use?
- In an existing project, follow what it already uses. Consistency matters more than the choice.
- In a new project,
.cppis the safest choice: every editor, IDE and tool recognises it. - Avoid a capital
.C, because it clashes with.con case-insensitive systems.
How to open and build a .cxx file
A .cxx file is plain text. You can read it in any text editor or IDE such as Visual Studio Code, Visual Studio, CLion or Notepad++. To build it with GCC, run g++ file.cxx -o program; the output name is up to you.
If a .cxx file does not open as text, check what it really is with our file header analyzer.
Related guides on Open-The-File.com
Bottom line
.cpp and .cxx are two spellings for the same thing. Pick one per project, stay consistent, and do not worry that either is "more correct" for the compiler.
