GCC Driver and Toolchain

Last update: 2021-06-21 17:17:24 +0800 +0800
Available translations: 日本語 / 中文(简体) / 中文(繁體)

gcc and g++ are GCC driver programs. They select a language front end from input suffixes and options, run preprocessing and compilation, invoke the assembler, and coordinate the linker. The GCC manual is authoritative because exact defaults depend on the configured target and GCC release.

Driver Stages and Language Defaults #

For a source file, the driver can stop after preprocessing (-E), compilation to assembly (-S), or assembly to an object (-c). Without a stopping option it links the resulting objects and libraries.

bash
g++ -std=c++20 -O2 -Wall -Wextra -c source.cc -o source.o
g++ source.o -o program

g++ selects C++ handling for relevant inputs and automatically adds the C++ runtime libraries at link time. Therefore use g++ for the final link of a C++ program, even when the objects were compiled separately. gcc is the C-oriented driver; passing -x c++ changes input language but does not by itself provide g++’s C++ link defaults.

Use -v to print driver activity while executing it. Use -### to print the commands the driver would execute without running them. -print-prog-name=cc1plus, -print-search-dirs, and -dumpspecs expose configured programs, search paths, and driver specifications for diagnosis rather than for portable build logic.

Dependencies and Intermediate Files #

Generate header dependencies from the compiler:

make
CPPFLAGS += -MMD -MP
OBJECTS := main.o parse.o

-include $(OBJECTS:.o=.d)

-MMD emits user-header dependencies while omitting system headers, and -MP adds phony targets for headers that may later be removed. Use -MF to choose a dependency file and -MT or -MQ to control its target. -save-temps preserves preprocessing and assembly intermediates when a stage needs inspection.

Linking, Specs, and Search Paths #

The driver expands specifications into commands that select startup objects, libgcc, language runtimes, and the linker. -specs=file changes that expansion and is a toolchain configuration mechanism, not a general project option.

-Bprefix changes the search prefix for subordinate programs and related files. --sysroot=dir relocates target headers and libraries for a cross toolchain. -I affects header lookup; -L adds a library directory; -lname selects a library. Link order matters for traditional static archives, so place an archive after objects that reference it. Pass linker-specific options through -Wl,option and keep build-system target configuration separate from host defaults.

Diagnostics and Reproducible Invocations #

Enable a documented warning policy, then promote selected warnings with -Werror= only when the project can maintain it across supported compilers. Add debug information with -g; use optimization levels deliberately because optimization changes code generation and can expose undefined behavior.

GCC can emit machine-readable diagnostics in configured formats, but availability changes by release. Query gcc --help=common and the matching installed manual before making JSON, SARIF, color, or path-format output a CI contract. Preserve the full driver invocation, compiler version, target triple, and relevant environment in failed-build logs because each participates in the effective toolchain.

Comments

Read or comment on GitHub (sign-in required to comment)