Skip to content

/commands/cpp/compile-cpp-program

g++ main.cpp -o main: compile and run a C++ program

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
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++:

bash
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 cpp

Output

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

OptionEffect
-o NAMEOutput file name (default a.out, or a.exe on MinGW)
-std=c++17 / c++20 / c++23Language standard; gnu++NN adds GNU extensions
-Wall -Wextra -WpedanticCommon warnings, extra warnings, strict ISO conformance
-O2 / -O3Optimization levels for release builds
-gDebug information for gdb or lldb
-cCompile to an object file only
-I DIR / -L DIR -lnameHeader search path / library path and library
-fsanitize=address,undefinedDetect memory errors and undefined behaviour at run time

Errors you will meet

Pitfalls

  • gcc instead of g++. gcc main.cpp compiles but fails to link with undefined references to std:: symbols.
  • Windows shells. In cmd.exe or PowerShell, run main.exe or .\main.exe; ./main is 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.

Sources