/commands/cpp/compile-cpp-program
g++ main.cpp -o main: compile and run a C++ program
- tool
- C++
- versions
- GCC 11+, Clang 14+, C++17/C++20/C++23
- os
- linux, macos, windows
g++ main.cpp -o main compiles a C++ source file into an executable named main; ./main runs it. Use g++ (or clang++), not gcc, so the C++ standard library is linked. Add -std=c++20 to choose the language standard, -Wall -Wextra for warnings and -O2 for an optimized build.
Compile and run
The example uses Clang 20 from the ziglang package on PyPI (Zig ships a complete Clang with libc++), run through uv; cxx below stands for it. On your machine, write g++ or clang++:
cxx() { uv run -q --no-project --python 3.13 --with ziglang==0.15.1 python -m ziglang c++ -fno-color-diagnostics "$@"; }
cat > main.cpp <<'EOF'
#include <iostream>
#include <string>
#include <vector>
#include <numeric>
int main(int argc, char* argv[]) {
std::vector<std::string> args(argv + 1, argv + argc);
std::vector<int> nums{1, 2, 3, 4};
int total = std::accumulate(nums.begin(), nums.end(), 0);
std::cout << "sum = " << total << ", args = " << args.size() << '\n';
for (const auto& a : args) std::cout << " " << a << '\n';
std::cout << "__cplusplus = " << __cplusplus << '\n';
}
EOF
cxx -std=c++20 -Wall -Wextra -O2 main.cpp -o main
./main
./main hello cppOutput
sum = 10, args = 0 __cplusplus = 202002 sum = 10, args = 2 hello cpp __cplusplus = 202002
__cplusplus shows the standard the file was compiled with: 202002 for C++20. main without a return statement returns 0 in C++.
Common options
| Option | Effect |
|---|---|
| -o NAME | Output file name (default a.out, or a.exe on MinGW) |
| -std=c++17 / c++20 / c++23 | Language standard; gnu++NN adds GNU extensions |
| -Wall -Wextra -Wpedantic | Common warnings, extra warnings, strict ISO conformance |
| -O2 / -O3 | Optimization levels for release builds |
| -g | Debug information for gdb or lldb |
| -c | Compile to an object file only |
| -I DIR / -L DIR -lname | Header search path / library path and library |
| -fsanitize=address,undefined | Detect memory errors and undefined behaviour at run time |
Errors you will meet
- Compile errors name the file and line: a missing declaration gives was not declared in this scope (GCC) or
use of undeclared identifier(Clang); a missing standard header no member named ... in namespace std; a wrong argument list no matching function for call to. - Link errors appear after compilation succeeded: undefined reference to means a function was declared but its definition (another
.cppfile or a library) was not linked. - Standard too old. Features such as
std::format(C++20) or structured bindings (C++17) fail on an older default; set the standard with -std.
Pitfalls
- gcc instead of g++.
gcc main.cppcompiles but fails to link with undefined references tostd::symbols. - Windows shells. In
cmd.exeor PowerShell, runmain.exeor.\main.exe;./mainis for Unix shells and Git Bash. - MSVC. Visual Studio's compiler uses different options (
cl /std:c++20 /EHsc main.cpp); the commands on this page are for GCC and Clang.
Confirm it worked
./main prints the expected output, and g++ --version shows which compiler built it. Recompile with -std=c++17 and check __cplusplus (201703) to see the standard switch in effect.
FAQ
Why does gcc main.cpp fail with undefined reference to std::cout?
gcc compiles .cpp files as C++ but does not link the C++ standard library. Use g++ (or clang++), which adds it automatically.
Which C++ standard does g++ use by default?
It depends on the version: GCC 11 and later default to gnu++17, Clang 16 and later also to gnu++17. Pass -std=c++20 or another value to be explicit.
How do I compile several .cpp files?
Pass them all: g++ main.cpp util.cpp -o app. Headers are included with #include and never passed on the command line; use -I DIR for header folders.