LinuxCommandLibrary

g++

Compile C++ source code

TLDR

Compile a source code file into an executable binary

$ g++ [path/to/source1.cpp path/to/source2.cpp ...] [[-o|--output]] [path/to/output_executable]
copy

Activate output of all errors and warnings
$ g++ [path/to/source.cpp] -Wall [[-o|--output]] [path/to/output_executable]
copy

Show common warnings, debug symbols in output, and optimize without affecting debugging
$ g++ [path/to/source.cpp] -Wall [[-g|--debug]] -Og [[-o|--output]] [path/to/output_executable]
copy

Choose a language standard to compile for (C++98/C++11/C++14/C++17)
$ g++ [path/to/source.cpp] -std=[c++98|c++11|c++14|c++17] [[-o|--output]] [path/to/output_executable]
copy

Include libraries located at a different path than the source file
$ g++ [path/to/source.cpp] [[-o|--output]] [path/to/output_executable] -I[path/to/header] -L[path/to/library] -l[library_name]
copy

Compile and link multiple source code files into an executable binary
$ g++ [[-c|--compile]] [path/to/source1.cpp path/to/source2.cpp ...] && g++ [[-o|--output]] [path/to/output_executable] [path/to/source1.o path/to/source2.o ...]
copy

Optimize the compiled program for performance
$ g++ [path/to/source.cpp] -O[1|2|3|fast] [[-o|--output]] [path/to/output_executable]
copy

Display version
$ g++ --version
copy

SYNOPSIS

g++ [options] input-file... [-o output-file]

PARAMETERS

-ansi
    Support ISO standard C++

-c
    Compile to object file only (no linking)

-E
    Preprocess only, output to stdout

-g
    Generate debug info for gdb

-o file
    Place output in file (default: a.out)

-Olevel
    Optimization level (level 0-3, s, fast, g)

-shared
    Create shared object (.so) library

-std=standard
    C++ dialect (c++98, c++11, c++17, c++20, c++2b)

-Wall
    Enable most common warnings

-Wextra
    Extra warnings

-Idir
    Add dir to include search path

-Ldir
    Add dir to library search path

-llib
    Link with library lib (e.g., -lpthread)

-fPIC
    Position-independent code for shared libs

-Dmacro[=def]
    Define macro macro as def (default 1)

-pthread
    Enable pthread support

-fsanitize=kind
    Address/thread sanitizer (kind: address, undefined)

--version
    Print compiler version

-v
    Verbose mode, show commands

-MM
    Output dependency makefile fragment

DESCRIPTION

g++ is the standard GNU C++ compiler frontend from the GNU Compiler Collection (GCC). It drives the full compilation pipeline: preprocessing (cpp), compilation (cc1plus), assembly (as), and linking (ld). By default, it compiles files with extensions like .cc, .cpp, .cxx, .C, .cp, or .c++ as C++ source code, automatically linking the libstdc++ standard library.
g++ supports modern C++ standards via -std=c++11, -std=c++17, -std=c++20, and experimental features. It provides optimization levels from -O0 (no opt) to -O3 and beyond, debugging with -g, code coverage (-fprofile-arcs), and sanitizers (-fsanitize=address).
Typical use: g++ -Wall -O2 -o myprog main.cpp utils.cpp. It excels in large projects with modules, templates, and multithreading support. Cross-compilation targets like arm-linux-gnueabi-g++ enable embedded development.

CAVEATS

Defaults to linking libstdc++; use gcc for C without -lstdc++. Large projects may need make or CMake. Extensions matter: .c is C, .cpp is C++. Watch for deprecated features in new standards.

INPUT EXTENSIONS

C++: .cc, .cpp, .cxx, .C, .cp, .c++, .ii, .ixx, .cppm, .c++m.
C: .c, .i. Assembler: .s, .S.

MULTI-FILE COMPILATION

Compile/link multiple: g++ *.cpp -o prog. Use -c first for objects, then g++ *.o -o prog.

PROFILING/DEBUG

-pg for gprof, -g -O0 for best debugging.

HISTORY

Developed by Richard Stallman as part of GCC 1.0 (May 1987). Initial C++ support via front-end in GCC 2.0 (1992). Evolved with C++ standards: C++98 (GCC 3.0, 2001), C++11 (GCC 4.7, 2012), C++20 (GCC 10, 2020). Now at GCC 14+ with modules and concepts.

SEE ALSO

gcc(1), cpp(1), ld(1), as(1), make(1), clang++(1)

Copied to clipboard