GCC
Purpose
Guide agents through GCC invocation: flag selection, build modes, warning triage, PGO, LTO, and common error patterns. Assume the project uses GNU Make, CMake, or a shell script.
Triggers
- "What flags should I use for a release build?"
- "GCC is giving me a warning/error I don't understand"
- "How do I enable LTO / PGO with GCC?"
- "How do I compile with
-fsanitize?" - "My binary is too large / too slow"
- Undefined reference errors, ABI mismatch, missing symbols
Workflow
1. Choose a build mode
Always pass -std=c11 / -std=c++17 (or the required standard) explicitly. Never rely on the implicit default.
2. Warning discipline
Start with -Wall -Wextra. For stricter standards compliance add -Wpedantic. To treat all warnings as errors in CI: -Werror.
Suppress a specific warning only in a narrow scope:
Do not pass -w (silences everything) except as a last resort for third-party headers.
3. Debug information
-g— DWARF debug info, default level 2-g3— includes macro definitions (useful with GDBmacro expand)-ggdb— DWARF extensions optimal for GDB-gsplit-dwarf— splits.dwofiles; reduces link time, needed fordebuginfod
Pair -g with -Og (not -O0) when you need readable optimised code in GDB.
4. Optimisation decision tree
-O3 vs -O2: -O3 adds aggressive loop transformations (-funswitch-loops, -fpeel-loops, -floop-interchange) and more aggressive inlining. Use -O3 only after benchmarking; it occasionally regresses due to i-cache pressure.
-Ofast: enables -ffast-math which breaks IEEE 754 semantics (NaN handling, associativity). Avoid unless the numerical domain explicitly permits it.
5. Link-time optimisation (LTO)
Use gcc-ar / gcc-ranlib instead of ar / ranlib when archiving LTO objects into static libs.
For parallel LTO: -flto=auto (uses make-style jobserver) or -flto=N.
See references/flags.md [blocked] for full flag reference. See skills/binaries/linkers-lto for linker-level LTO configuration.
6. Profile-guided optimisation (PGO)
-fprofile-correction handles profile data inconsistencies from multi-threaded runs.
7. Preprocessor and standards
- Inspect macro expansion:
gcc -E file.c | less - Dump predefined macros:
gcc -dM -E - < /dev/null - Force strict standard:
-std=c11 -pedantic-errors - Disable GNU extensions:
-std=c11(not-std=gnu11)
8. Common error triage
For sanitizer reports, use skills/runtimes/sanitizers.
9. Useful one-liners
For a complete flag cheatsheet, see references/flags.md [blocked]. For common error patterns and examples, see references/examples.md [blocked].
Related skills
- Use
skills/runtimes/sanitizersto add-fsanitize=*builds - Use
skills/compilers/clangwhen switching to clang/LLVM - Use
skills/binaries/linkers-ltofor advanced LTO linker flags - Use
skills/debuggers/gdbfor debugging GCC-built binaries


