g++ — C++ compiler
Development / Build / g++.html
The C++ front end of GCC; compiles C++ sources into objects or executables.
Basic Information
| Command Name | g++ |
|---|---|
| One-line Introduction | The C++ front end of GCC; compiles C++ sources into objects or executables. |
| Provided by | GCC (GNU Compiler Collection) |
| Official Documentation | https://gcc.gnu.org/onlinedocs/ |
| Supported Operating Systems | Linux, macOS, BSD, Windows (separate install) |
| Installation Status | Requires a package (build-essential or g++) |
| Category | Build and compile (tags: Language toolchain (tags: g++, c++, compile, gcc)) |
What is this command?
g++ translates C++ sources into machine code, running preprocessing, compilation, assembly and linking in one invocation. Use -c for objects only and -o to name the output.
Be explicit about warnings and optimization. -Wall -Wextra catches problems early, while release builds typically use -O2 and drop debug symbols. Library order is a common cause of link errors.
What problems does it solve?
- Compiling C++ sources to check behavior quickly
- Building objects separately for incremental builds
- Raising warning levels to catch latent bugs
- Verifying compatibility with a specific standard
Key features
Single-file build
Compile one source into an executable in one step.
- When to use: Trying examples or small tools
- What you get: You get a runnable binary immediately.
- Limitations: Library dependencies are not resolved automatically.
Warnings and standard
Use -Wall -Wextra and -std for quality and compatibility.
- When to use: Writing portable code
- What you get: Problems surface at compile time.
- Limitations: Warnings alone do not fail the build without -Werror.
Separate objects
Create objects with -c and link them at the end.
- When to use: Incremental builds of larger projects
- What you get: Only changed files are recompiled.
- Limitations: You must manage link order and duplicate symbols.
Installation and usage
Check whether it is installed
g++ --version
sudo apt install build-essential # Debian/Ubuntu sudo dnf install gcc-c++ # Fedora
Real-world examples
Compile one file
Enable warnings and produce an executable.
g++ -Wall -Wextra -O2 -std=c++20 main.cpp -o app
./app
Create an object file
Compile only and link later.
g++ -c util.cpp -o util.o
g++ main.o util.o -o app
Link a library
Link a library that is not included by default.
g++ calc.cpp -o calc -lm