skip to content

Driver and Stages

The g++ driver runs the preprocessor, compiler proper, assembler and linker, and -E, -S and -c stop it early. Interviewers ask what each stage outputs and what -g adds.

on this pageshow

explore

questions

5

What does GCC's -g option add to an object file, and how do -g1, -g3 and -ggdb differ?

level: juniorimportance: must knowfreq 60%

answer

  1. extra sections, same instructions
  2. DWARF: .debug_info, .debug_line
  3. levels 0 to 3, default 2
  4. level 3 adds macros
  5. last -g option wins

basics

~20 s

-g adds debugging information, normally DWARF sections describing types, variables and line numbers, without changing the generated instructions. -g1 keeps functions, external variables and line tables, the default -g2 adds locals, -g3 adds macro definitions, and -ggdb prefers GDB extensions.

solid answer

~40 s

`-g` tells GCC to emit **debug information** in the platform's native format, which on Linux is DWARF: sections such as `.debug_info`, `.debug_line` and `.debug_abbrev` that map addresses back to files, lines, functions, types and variables. It does not change the instructions GCC generates; the manual even documents `-fcompare-debug`, which compiles twice with debug info toggled and errors if the final representation differs. The level selects how much is recorded: `-g0` none, `-g1` functions, external variables and line tables but no locals, `-g2` (plain `-g`) adds locals and C++ namespace, class and template detail, and `-g3` adds every macro definition. `-ggdb` asks for the most expressive format including GDB extensions. `-g` combines freely with `-O2`, though optimized code may show variables as optimized out.

code

bash · 6 lines
bash
g++ -O2 -c scale.cpp -o plain.o
g++ -O2 -g -c scale.cpp -o debug.o
objdump -h debug.o | grep debug     # .debug_info, .debug_line, ... appear
objdump -d plain.o > a.txt
objdump -d debug.o > b.txt
diff <(tail -n +3 a.txt) <(tail -n +3 b.txt) && echo 'same instructions'

go deeper

for a junior

Say that -g adds debug sections for the debugger, that plain -g means level 2, and that -g3 adds macros.

for a middle

Explain that -g leaves the instructions unchanged, what each level records, and why the last -g option on the command line wins.

for a senior

Ship optimized builds with debug information split off for symbolization, and know what optimized code does to variables shown in the debugger.

for a principal

Set a team policy for debug-info levels per build type and for archiving the split debug files that match every released binary.

## What -g actually adds Debug information is **metadata about the code**, stored beside it. On ELF platforms GCC writes it as DWARF (version 5 by default on most targets, per the GCC 16 manual) in sections the loader never maps for execution: - `.debug_info`: a tree of compile units, functions, variables, types, namespaces and template instances; - `.debug_line`: the table that maps every instruction address back to a file and line; - `.debug_abbrev`, `.debug_str` and friends: supporting tables; - `.debug_macro`: macro definitions, only at level 3. A debugger uses these tables to turn a crash address into `parser.cpp:142`, print a local variable by name, or step one source line at a time. Without them it sees only symbols from the symbol table and raw addresses. ## It does not change the code `-g` is designed to leave the instructions alone. Compiling the same file with and without `-g` produces the same `.text`; only the debug sections differ, which makes the object file larger. GCC backs this with `-fcompare-debug`, which runs a second compilation with debug information toggled and reports an error if the final internal representation differs. That is why shipping a release built with `-O2 -g` costs file size, not speed, and why many teams build release binaries with debug information and then strip it into a separate file. ## The levels | Option | Records | Typical use | |---|---|---| | `-g0` | nothing; cancels an earlier `-g` | override in one target | | `-g1` | functions, external variables, line tables; **no locals** | backtraces from code you do not plan to step through | | `-g` / `-g2` | level 1 plus local variables and typedefs, C++ namespace, class and template detail | normal debugging | | `-g3` | level 2 plus **all macro definitions** | expanding macros in the debugger | Two rules the manual spells out: 1. The default level is 2, so `-g` and `-g2` are the same. 2. If several `-g` options appear, **the last one wins**. A build system that appends `-g1` after your `-g3` silently drops the macro data. ## Format selectors The level and the format are separate knobs: - `-ggdb` produces the most expressive format available, including **GDB extensions** where possible; `-ggdb3` combines that with level 3. - `-gdwarf-4` or `-gdwarf-5` pins a DWARF version for older consumers; the level is then set with a separate `-g2` or `-g3`, because `-gdwarf` does not accept a concatenated level. - `-gsplit-dwarf` moves most DWARF into a `.dwo` file per object, so the linker handles far less debug data in large projects. Clang shares `-g`, `-g1`, `-g3` and `-ggdb`, but its `-g3` emits macro information only with `-fdebug-macro`; GCC's level 3 records macros by itself. ## Debug information with optimization GCC allows `-g` with any `-O` level. The manual is honest about the result: variables may not exist at all, control flow may jump around, and statements may run in a different place because they were moved out of loops. Expect the debugger to report values as optimized out. For an edit-compile-debug loop, `-Og -g` keeps most of the speed while preserving a usable view, which is covered with optimization levels. ## Size, and keeping it out of the shipped binary Debug information is often several times larger than the code it describes, especially for template-heavy C++, where every instantiation gets its own type and function entries. The usual handling: 1. Build with `-g` so the exact binary has full debug information. 2. Copy the debug sections into a separate file and strip them from the binary you ship, keeping the two linked by a build ID. 3. Archive the debug file with the release, so a crash address from a customer can later be mapped to a file and line. Within the build, `-gsplit-dwarf` reduces link time and memory by leaving most DWARF in per-object `.dwo` files the linker never reads, and `-gz` compresses debug sections where the toolchain supports it. None of these choices changes the code that runs. ## Practical checklist - Debug builds: `-g` or `-g3` with `-O0` or `-Og`. - Release builds: `-O2 -g`, then split the debug sections off the shipped binary so crash reports can be symbolized later. - Huge links: consider `-gsplit-dwarf`. - Remember that `-g` must be present **at compile time**; adding it only to the link line produces no line tables for objects compiled without it.

  • Why does a release build often use -O2 -g instead of dropping -g?
    Debug information does not change the instructions, so `-O2 -g` runs exactly like `-O2`. Keeping the DWARF, usually stripped into a separate file that never ships, lets you symbolize crash reports and core dumps from the exact binary customers run.
  • What happens if CMake adds -g and your own flags add -g1 later on the command line?
    GCC uses the last `-g` option it sees, so the build gets level 1: line tables and functions but no local variables. Debugging then shows backtraces but cannot print locals, which looks like a debugger fault until you read the full command line.

  • Adding -g makes the program run slower because it changes the generated code.
  • -g cannot be combined with -O2, so release builds can never carry debug information.
  • -g3 is a stronger optimization of debug information than -g.
  • Passing -g only at link time is enough to get line numbers.
  • -g1 is the default level used by plain -g.
open as a page

What stages does the g++ driver run, and where do the -E, -S and -c options stop it?

level: juniorimportance: must knowfreq 72%

basics

~20 s

g++ is a driver that runs the preprocessor, the compiler proper, the assembler and the linker in that order. -E stops after preprocessing, -S after producing assembly (.s), and -c after assembling an object file (.o).

open as a page

With GCC, in what order are -iquote, -I and -isystem directories searched, and how do -D and -U define macros?

level: middleimportance: should knowfreq 55%

basics

~20 s

For #include "..." GCC searches the including file's directory, then -iquote, then -I, then -isystem and the system directories; #include <...> starts at -I. -D name defines it as 1, -D name=value sets a value, and -U cancels a definition.

open as a page

What C++ standard does GCC 16 compile by default, and how does -std=gnu++20 differ from -std=c++20?

level: middleimportance: should knowfreq 45%

basics

~20 s

GCC 16 defaults to -std=gnu++20, up from gnu++17 in GCC 11 to 15. The gnu++ dialects keep GNU extensions that conflict with ISO C++; -std=c++20 disables those, but other extensions remain until -Wpedantic diagnoses them.

open as a page

A GCC build mis-expands a macro and an inline asm block returns garbage; how do -E, -dD, -save-temps and -S -fverbose-asm show what the compiler received and emitted?

level: seniorimportance: should knowfreq 35%

basics

~20 s

Rerun the exact compile line with -E, adding -dD or -dM, to see expanded code and macro definitions, or with -save-temps to keep the .ii and .s. Then read -S -fverbose-asm output for the registers GCC gave the inline asm.

open as a page