LinuxCommandLibrary

c++

Compile C++ programs

TLDR

View documentation for original command

$ tldr g++
copy

SYNOPSIS

c++ [options] source_files...

PARAMETERS

-o file
    Specify the output file name for the executable, object file, or library.

-c
    Compile or assemble the source files, but do not link. Produces object files (.o).

-I dir
    Add the specified directory to the list of directories to be searched for header files.

-L dir
    Add the specified directory to the list of directories to be searched for libraries.

-llibrary
    Link with the specified library. For example, -lm links with the math library.

-Dmacro[=def]
    Define a preprocessor macro. Equivalent to #define macro def in source code.

-O[level]
    Set the optimization level. Common levels include -O0 (no optimization), -O1, -O2, -O3 (more aggressive optimization), and -Os (optimize for size).

-std=standard
    Specify the C++ standard to compile against, e.g., c++11, c++17, c++20.

-Wall
    Enable most common compiler warning messages.

-Wextra
    Enable additional compiler warning messages not covered by -Wall.

-g
    Include debugging information in the compiled output, useful for debuggers like GDB.

-v
    Print the commands executed by the compiler driver and show version information.

DESCRIPTION

The c++ command on Linux systems typically acts as a convenient frontend or alias to the system's default C++ compiler, most commonly g++ (GNU C++ Compiler) or sometimes clang++ (Clang C++ Compiler).

It is used to compile C++ source code files into executable programs, object files, or shared libraries. The compilation process involves several stages: preprocessing (handling directives like #include), compilation (translating C++ code into assembly), assembly (converting assembly into machine code/object files), and linking (combining object files and libraries into an executable or shared library).

By using the c++ command, developers can invoke the underlying C++ compiler with various options to control optimization levels, include directories, library linking, warning messages, debugging information, and the C++ standard to be used, simplifying the build process for C++ projects.

CAVEATS

The exact behavior and available options of the c++ command depend entirely on the underlying compiler it invokes (e.g., g++ or clang++). It is often a symbolic link, and its configuration can vary between Linux distributions. Users should be aware of the specific compiler version and implementation being used for complex projects, especially when dealing with compiler-specific extensions or warnings.

DEFAULT BEHAVIOR

When invoked without specific output options, c++ will attempt to compile and link all specified source files, producing an executable named a.out in the current directory. This default is often overridden using the -o option.

PREPROCESSING

The c++ command implicitly performs preprocessing. To see only the preprocessed output of a source file, one can use the -E option, which prints the result to standard output.

HISTORY

The concept of a C++ compiler emerged with the development of the C++ language itself in the early 1980s. The GNU Project's g++ compiler, part of the GNU Compiler Collection (GCC), became widely adopted on Linux and other Unix-like systems, providing a free and open-source implementation. Later, Clang, an LLVM-based compiler, gained prominence as an alternative. The c++ command serves as a convenient, generic invocation point for whichever C++ compiler is configured as the system default, often being a symlink to either g++ or clang++, ensuring consistency across different compiler installations and simplifying build scripts.

SEE ALSO

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

Copied to clipboard