g++
Compile C++ source code
TLDR
Compile a source code file into an executable binary
Activate output of all errors and warnings
Show common warnings, debug symbols in output, and optimize without affecting debugging
Choose a language standard to compile for (C++98/C++11/C++14/C++17)
Include libraries located at a different path than the source file
Compile and link multiple source code files into an executable binary
Optimize the compiled program for performance
Display version
SYNOPSIS
g++ [options] files...
PARAMETERS
-o
Specify the name of the output file (executable or library).
-c
Compile or assemble the source files, but do not link them. Generates object files (.o).
-S
Compile only; do not assemble or link. Generates assembly code files (.s).
-E
Preprocess only; do not compile, assemble, or link. Prints the preprocessed source code to standard output.
-Wall
Enable all commonly used warning options, encouraging robust code.
-Wextra
Enable extra warning flags, in addition to those enabled by -Wall.
-g
Produce debugging information in the output file, essential for use with debuggers like GDB.
-O[0-3]
Specify the optimization level. -O0 (no optimization), -O1 (moderate), -O2 (more), -O3 (most aggressive).
-std=
Specify the C++ language standard to compile against (e.g., c++11, c++17, gnu++20).
-I
Add dir to the list of directories to be searched for header files.
-L
Add dir to the list of directories to be searched for libraries.
-l
Link with the library named lib. For example, -lm links with libm.a or libm.so.
-D
Define a preprocessor macro. Equivalent to #define in source code.
-v
Print verbose information about the compilation stages and the commands executed.
-shared
Create a shared library (e.g., .so file) instead of an executable.
DESCRIPTION
g++ is the GNU C++ compiler, a crucial component of the GNU Compiler Collection (GCC). It serves as the primary tool for compiling C++ source code into executable programs, shared libraries, or object files on Linux and other Unix-like operating systems.
It handles the entire compilation process, from preprocessing source files and compiling them into assembly, to assembling the assembly into machine code, and finally linking object files with necessary libraries to produce the final output.
g++ supports various C++ language standards (e.g., C++11, C++17, C++20), allowing developers to write modern C++ applications. It provides extensive options for optimization, debugging, warning control, and managing include and library paths, making it a flexible and powerful development tool. It automatically links standard C++ runtime libraries, simplifying the build process for most C++ projects.
CAVEATS
Compiling very large projects can be resource-intensive in terms of CPU and memory.
Linker errors (e.g., 'undefined reference') can be particularly cryptic, often requiring careful examination of library dependencies and symbol visibility.
Binaries compiled with specific g++ versions or flags might not be fully compatible or portable across different Linux distributions or older systems without proper dependency management.
COMPILATION PHASES
When you invoke g++, it typically orchestrates a series of distinct compilation phases:
1. Preprocessing: Handles preprocessor directives (e.g., #include, #define) to expand macros and include header files.
2. Compiling: Translates the preprocessed C++ source code into assembly language specific to the target architecture.
3. Assembling: Converts the assembly code into machine-executable object files (.o), which are binary representations of individual source files.
4. Linking: Combines all generated object files with necessary external libraries (both static and shared) to create the final executable program or shared library.
ERROR AND WARNING MESSAGES
g++ provides detailed feedback during compilation. Warnings (e.g., 'warning: unused variable') indicate potential issues that might not prevent compilation but suggest suboptimal or risky code. Errors (e.g., 'error: expected declaration before ‘}’ token') signify critical problems that halt the compilation process. Messages usually include the file name, line number, and column number, making debugging easier.
HISTORY
g++ is the C++ front-end for the GNU Compiler Collection (GCC), which was originally initiated by Richard Stallman as part of the GNU Project in the mid-1980s. While GCC's initial focus was on the C language, C++ support was quickly integrated, with g++ emerging as the dedicated command for C++ compilation. Its development has consistently kept pace with the evolution of the C++ language standard, rapidly adopting features from new standards like C++11, C++17, and C++20. g++ has become the de facto standard compiler for C++ development on Linux and other Unix-like systems, widely adopted for its open-source nature, robustness, and comprehensive support for the language.