
Gcc
- 393 installs
- 155 repo stars
- Updated June 27, 2026
- mohitmishra786/low-level-dev-skills
gcc is an agent skill that guides GNU Compiler Collection flag selection, diagnostics, LTO, PGO, and sanitizer setup for developers who compile optimized C/C++ native libraries, CLIs, and JNI bridges.
About
gcc is an agent skill from mohitmishra786/low-level-dev-skills, a curated suite for systems programming across C/C++, Rust, and Zig toolchains. The skill walks agents through GCC invocations: debug builds with -g -O0 -Wall -Wextra, release -O2/-O3 profiles, link-time optimization, profile-guided optimization, and -fsanitize=address,undefined instrumentation. It covers warning triage, undefined-reference and ABI mismatch errors, preprocessor macros, and integration with GNU Make, CMake, or shell build scripts. The parent repo groups gcc among 8 dedicated C/C++ compiler skills alongside clang, llvm, cross-gcc, and pgo. Developers reach for gcc when a native build is too slow, too large, too noisy with warnings, or failing at link time—and they need concrete flag recipes instead of guessing optimization levels.
- Warning and optimization flags
- Static vs shared linking
- LTO and PGO basics
- Debug symbol control
- Multistage Makefile/CMake usage
Gcc by the numbers
- 393 all-time installs (skills.sh)
- +26 installs in the week ending Aug 4, 2026 (Skillselion tracking)
- Ranked #140 of 550 CLI & Terminal skills by installs in the Skillselion catalog
- Data as of Aug 4, 2026 (Skillselion catalog sync)
npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill gccAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 393 |
|---|---|
| repo stars | ★ 155 |
| Last updated | June 27, 2026 |
| Repository | mohitmishra786/low-level-dev-skills ↗ |
Which GCC flags should I use for release builds?
Configure GCC flags, warnings, LTO, and link steps to compile optimized native libraries, CLIs, and mobile JNI bridges reliably.
Who is it for?
Systems and backend developers compiling C/C++ with GCC via Make, CMake, or shell scripts who need correct debug, release, LTO, PGO, or sanitizer flag sets.
Skip if: Pure JavaScript or managed-language projects with no native compilation step, or teams standardized exclusively on Clang/MSVC without GCC.
When should I use this skill?
User asks about gcc flags, compilation errors, LTO, PGO, -fsanitize, warning suppression, or optimizing a slow or oversized GCC binary.
What you get
Optimized compile and link command lines with chosen -O levels, warning flags, LTO/PGO steps, and sanitizer instrumentation for C/C++ targets.
- GCC compile and link command lines
- Optimized warning and sanitizer flag sets
By the numbers
- Part of low-level-dev-skills with 8 dedicated C/C++ compiler skills including gcc and clang
- Documents 5 build-mode flag profiles from debug (-O0) through release (-O3) and -Os
Files
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
| Goal | Recommended flags |
|---|---|
| Debug | -g -O0 -Wall -Wextra |
| Debug + debuggable optimisation | -g -Og -Wall -Wextra |
| Release | -O2 -DNDEBUG -Wall |
| Release (max perf, native only) | -O3 -march=native -DNDEBUG |
| Release (min size) | -Os -DNDEBUG |
| Sanitizer (dev) | -g -O1 -fsanitize=address,undefined |
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:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
// ...
#pragma GCC diagnostic popDo 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
Need max throughput on a fixed machine?
yes -> -O3 -march=native -flto
no -> profiling available?
yes -> -O2 -fprofile-use
no -> -O2
Size-constrained (embedded, shared lib)?
yes -> -Os (or -Oz with clang)-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)
# Compile
gcc -O2 -flto -c foo.c -o foo.o
gcc -O2 -flto -c bar.c -o bar.o
# Link (must pass -flto again)
gcc -O2 -flto foo.o bar.o -o progUse 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 for full flag reference. See skills/binaries/linkers-lto for linker-level LTO configuration.
6. Profile-guided optimisation (PGO)
# Step 1: instrument
gcc -O2 -fprofile-generate prog.c -o prog_inst
# Step 2: run with representative workload
./prog_inst < workload.input
# Step 3: optimise with profile
gcc -O2 -fprofile-use -fprofile-correction prog.c -o prog-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
| Symptom | Likely cause | Fix |
|---|---|---|
undefined reference to 'foo' | Missing -lfoo or wrong link order | Add -lfoo; move -l flags after object files |
multiple definition of 'x' | Variable defined (not just declared) in a header | Add extern in header, define in one .c |
implicit declaration of function | Missing #include | Add the header |
warning: incompatible pointer types | Wrong cast or missing prototype | Fix the type; check headers |
| ABI errors with C++ | Mixed -std= or different libstdc++ | Unify -std= across all TUs |
relocation truncated | Overflow on a 32-bit relative relocation | Use -mcmodel=large or restructure code |
For sanitizer reports, use skills/runtimes/sanitizers.
9. Useful one-liners
# Show all flags enabled at -O2
gcc -Q --help=optimizers -O2 | grep enabled
# Preprocess only (check includes/macros)
gcc -E -dD src.c -o src.i
# Assembly output (Intel syntax)
gcc -S -masm=intel -O2 foo.c -o foo.s
# Show include search path
gcc -v -E - < /dev/null 2>&1 | grep -A20 '#include <...>'
# Check if a flag is supported
gcc -Q --help=target | grep marchFor a complete flag cheatsheet, see references/flags.md. For common error patterns and examples, see references/examples.md.
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
GCC Examples and Error Patterns
Table of Contents
1. Standard build recipes 2. Common error patterns 3. Preprocessor inspection 4. Assembly output
---
Standard build recipes
Debug build
gcc -std=c11 -g -Og -Wall -Wextra -Wpedantic -o prog src/*.cRelease build
gcc -std=c11 -O2 -DNDEBUG -Wall -fstack-protector-strong -D_FORTIFY_SOURCE=2 \
-o prog src/*.cShared library
gcc -std=c11 -O2 -fPIC -Wall -shared -Wl,-soname,libfoo.so.1 \
-o libfoo.so.1.0 foo.c
ln -sf libfoo.so.1.0 libfoo.so.1
ln -sf libfoo.so.1 libfoo.soFreestanding (embedded)
arm-none-eabi-gcc -std=c11 -O2 -g -Wall \
-mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 \
-ffreestanding -nostdlib \
-T linker.ld -o firmware.elf startup.s main.c---
Common error patterns
undefined reference to 'foo'
Cause: Linker cannot find the symbol. Most common reasons:
1. Missing -lfoo flag 2. Library listed before the object that needs it
# Wrong: library before object
gcc main.o -lz -o prog # if main.o uses zlib this can fail
# Correct
gcc main.o -o prog -lz # -l flags go after objectsmultiple definition of 'x'
A variable is defined (not just declared) in a header included by multiple TUs.
// header.h — wrong
int counter = 0; // definition
// header.h — correct
extern int counter; // declaration only
// counter.c
int counter = 0; // one definitionimplicit declaration of function (C99+: error)
Missing #include. Add the header or forward-declare the function.
# Find which header declares a function
man 3 strdup | grep SYNOPSISrelocation truncated to fit: R_X86_64_PC32
Binary exceeds 2 GB or a symbol is out of range for a 32-bit-relative relocation.
# Fix: use large code model
gcc -mcmodel=large -O2 -o prog ...ABI mismatch (C++)
Mixing objects compiled with different -std=c++NN or different libstdc++ versions. Symptoms: std::string or std::list methods missing at link time.
Fix: compile all TUs with the same -std= and link against the same runtime.
Stack overflow / SIGSEGV in signal handler
Red zone use in signal handlers on x86-64. Fix: -mno-red-zone for kernel/signal code.
---
Preprocessor inspection
# Full preprocessed output
gcc -E src.c -o src.i
# Show all predefined macros
gcc -dM -E - < /dev/null
# Show macros + includes in order
gcc -dD -E src.c
# Check what __GNUC__ is
gcc -dM -E - < /dev/null | grep GNUC
# Find include search paths
gcc -v -E - < /dev/null 2>&1 | sed -n '/#include </,/End of search/p'---
Assembly output
# Default (AT&T) syntax
gcc -S -O2 foo.c -o foo.s
# Intel syntax
gcc -S -masm=intel -O2 foo.c -o foo.s
# Interleaved C source + assembly
gcc -S -O2 -fverbose-asm foo.c -o foo.s
# From object: disassemble with objdump
objdump -d -M intel -S foo.o # -S intermixes source (needs -g)GCC Flag Reference
Source: <https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html> Source: <https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html> Source: <https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html>
Table of Contents
1. Optimisation flags 2. Debug flags 3. Warning flags 4. Hardening flags 5. Code generation flags 6. LTO / PGO flags 7. Diagnostic flags
---
Optimisation flags
| Flag | Enables | Notes |
|---|---|---|
-O0 | Nothing | Default; best for debugging |
-O1 | Basic DCE, CSE, register allocation | ~40 sub-passes |
-O2 | -O1 + vectorisation, inlining, devirtualisation | Standard release |
-O3 | -O2 + loop transformations, aggressive inlining | Benchmark before using |
-Os | -O2 minus size-increasing passes + -finline-functions | Shared libs, embedded |
-Og | Subset of -O1 safe for debugging | Use with -g in debug cycle |
-Ofast | -O3 + -ffast-math + -fallow-store-data-races | Breaks IEEE 754; avoid unless sure |
-Oz | More aggressive than -Os | clang only; GCC has -Os |
Key sub-flags (fine-tuning)
| Flag | Default at | Effect |
|---|---|---|
-march=native | off | Tune for host CPU; not portable |
-mtune=native | off | Schedule for host without changing ISA |
-fomit-frame-pointer | -O1+ | Frees one register; breaks naive profiling |
-funroll-loops | off | Unroll loops; often hurts i-cache |
-fstrict-aliasing | -O2+ | Assume no aliasing across types (C §6.5p7) |
-fno-strict-aliasing | off | Disable; needed for some legacy C code |
-fvisibility=hidden | off | Default symbol visibility; reduces DSO size |
-ffunction-sections -fdata-sections | off | One section per symbol; enable dead-code stripping with --gc-sections |
---
Debug flags
| Flag | Effect |
|---|---|
-g | DWARF level 2 (default) |
-g1 | Minimal debug (function names, line numbers only) |
-g2 | Default; local vars, types |
-g3 | Includes macro definitions |
-ggdb | DWARF extensions for GDB |
-ggdb3 | GDB extensions + macros |
-gsplit-dwarf | Separate .dwo files; faster linking |
-gz | Compress debug sections (zlib) |
-fno-eliminate-unused-debug-types | Keep all types in DWARF |
-fdebug-prefix-map=old=new | Remap paths in debug info (reproducible builds) |
---
Warning flags
Recommended baseline
-Wall -Wextra -WpedanticAdditional useful warnings
| Flag | Catches |
|---|---|
-Wshadow | Local variable shadows outer scope |
-Wcast-align | Unaligned pointer casts |
-Wcast-qual | Casting away const/volatile |
-Wconversion | Implicit narrowing conversions |
-Wdouble-promotion | float implicitly promoted to double |
-Wformat=2 | Printf/scanf format string issues (stricter than -Wformat) |
-Wnull-dereference | Pointer deref that could be NULL |
-Wstack-usage=N | Functions using more than N bytes of stack |
-Wundef | Undefined identifiers used in #if |
-Wunreachable-code | Code after return/break (GCC extension) |
-fanalyzer | GCC 10+ static analyser; slow but finds real bugs |
C++-specific
| Flag | Catches |
|---|---|
-Woverloaded-virtual | Hiding base class virtual function |
-Wnon-virtual-dtor | Class with virtual functions but non-virtual destructor |
-Weffc++ | Effective C++ guidelines (noisy) |
-Wold-style-cast | C-style casts in C++ code |
---
Hardening flags
For production binaries (defence-in-depth):
-D_FORTIFY_SOURCE=2 # Buffer overflow detection in libc calls
-fstack-protector-strong # Stack canary (balanced: protects most frames)
-fstack-protector-all # Every function (slower)
-fPIE -pie # Position-independent executable (ASLR)
-Wl,-z,relro -Wl,-z,now # Read-only relocations, bind now (RELRO full)
-fcf-protection=full # Intel CET shadow stack + IBT (x86 only)Do NOT combine -fstack-protector-strong with -fno-stack-protector on the same TU.
---
Code generation flags
| Flag | Effect |
|---|---|
-fPIC | Position-independent code (shared library objects) |
-fPIE | Position-independent executable |
-shared | Build a shared library |
-static | Link statically |
-nostdlib | Do not link standard libraries (embedded/freestanding) |
-ffreestanding | Freestanding environment; no hosted stdlib assumed |
-mcmodel=small/medium/large | x86-64 code model; use large if >2 GB image |
-mno-red-zone | Disable x86-64 128-byte red zone (needed in kernel code) |
---
LTO / PGO flags
| Flag | Phase | Effect |
|---|---|---|
-flto | compile+link | Enable LTO |
-flto=auto | compile+link | Parallel LTO via jobserver |
-flto=N | compile+link | N parallel LTO jobs |
-fprofile-generate | compile | Instrument for PGO |
-fprofile-use | compile | Apply .gcda profile data |
-fprofile-correction | compile | Handle inconsistent profiles (parallel workloads) |
-fauto-profile=file | compile | AutoFDO from perf sampling (Linux only) |
Use gcc-ar / gcc-ranlib when creating archives of LTO objects.
---
Diagnostic flags
| Flag | Effect |
|---|---|
-fdiagnostics-color=auto | Colour output when terminal supports it |
-fmax-errors=N | Stop after N errors |
-Q --help=optimizers -O2 | List all optimisation passes enabled at -O2 |
-v | Verbose: show subcommands, include paths, library paths |
-### | Show subcommands without executing |
-save-temps | Keep .i, .s intermediates |
-Wa,-adhln=foo.lst | Produce assembly listing alongside object |
-Rpass=inline | (clang-style; GCC does not have -Rpass directly) |
Related skills
How it compares
Pick gcc over generic C++ advice when the toolchain is specifically GNU GCC and you need flag tables for LTO, PGO, sanitizers, and Make/CMake integration.
FAQ
What GCC flags does the gcc skill recommend for debug builds?
The gcc skill recommends -g -O0 -Wall -Wextra for full debuggability, or -g -Og -Wall -Wextra when some optimization is needed while keeping binaries easy to debug in GDB.
Does the gcc skill cover link-time optimization?
Yes. The gcc skill documents LTO and profile-guided optimization workflows, including when to enable -flto for release builds and how to integrate PGO collect and use phases with Make or CMake.
Which build systems does the gcc skill assume?
The gcc skill assumes projects build with GNU Make, CMake, or shell scripts. It focuses on flag selection, warning triage, and common link errors rather than replacing those build systems.