LinuxCommandLibrary

c++

Compile C++ programs

TLDR

View documentation for the original command

$ tldr g++
copy

SYNOPSIS

c++ [options] input-files...

PARAMETERS

-o file
    Specify output file name (default: a.out)

-c
    Compile to object file only, no linking

-g
    Generate debugging information (for gdb)

-O0|-O1|-O2|-O3
    Optimization levels (0=none, 3=max)

-Wall
    Enable most common warnings

-Wextra
    Enable additional warnings

-std=std
    Specify C++ standard (e.g., c++11, c++17, c++20)

-Idir
    Add directory to header search path

-Ldir
    Add directory to library search path

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

-shared
    Produce shared object (.so) file

-fPIC
    Generate position-independent code

-pthread
    Support POSIX threads

-v
    Verbose output, show commands

--version
    Display compiler version

DESCRIPTION

The c++ command serves as the standard driver for the C++ compiler on Linux, typically a symlink to g++ from the GCC toolchain. It compiles C++ source files (e.g., .cpp, .cc, .cxx, .C) into object code or executables by invoking the preprocessor, compiler, assembler, and linker.

Usage involves specifying source files and options to control optimization, debugging, standards compliance, and linking. For example, c++ main.cpp -o main produces an executable named main. It supports C++ standards via -std=c++11, -std=c++17, up to -std=c++23 or gnu++23 depending on GCC version.

Key features include static/dynamic linking, library paths with -L and -l, header inclusion via -I, and warnings/debug symbols. It integrates with build systems like Make or CMake. While powerful, it requires GCC installation (e.g., sudo apt install g++ on Debian-based systems), and output depends on architecture (e.g., x86_64).

c++ excels in performance-critical code but demands knowledge of flags for best results. It's ubiquitous in software development, from CLI tools to large projects.

CAVEATS

Not standalone; symlinks to versioned g++ (e.g., g++-12). Behavior varies by GCC version. Linking requires runtime libs like libstdc++. Use pkg-config for complex dependencies.

INPUT EXTENSIONS

Recognizes .cc, .cpp, .cxx, .C, .c++ as C++; .c as C.

PREPROCESSING

Implicitly runs cpp; use -E to stop after preprocessing.

MULTI-FILE

Compiles/links multiple files: c++ file1.cpp file2.cpp -o prog.

HISTORY

Introduced with GCC 2.95 (1999) as c++ frontend; evolved alongside C++ standards (ISO/IEC 14882 since 1998). GCC originated 1987 by Richard Stallman; c++ symlinks standardized in FHS for distros like Ubuntu/Debian since ~2004.

SEE ALSO

g++(1), gcc(1), clang++(1), ld(1), make(1)

Copied to clipboard