$npx -y skills add mohitmishra786/low-level-dev-skills --skill gccGCC compiler skill for C/C++ projects. Use when selecting optimization levels, warning flags, debug builds, LTO, sanitizer instrumentation, or diagnosing compilation errors with GCC. Covers flag selection for debug vs release, ABI concerns, preprocessor macros, profile-guided opt
| 1 | # GCC |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | 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. |
| 6 | |
| 7 | ## Triggers |
| 8 | |
| 9 | - "What flags should I use for a release build?" |
| 10 | - "GCC is giving me a warning/error I don't understand" |
| 11 | - "How do I enable LTO / PGO with GCC?" |
| 12 | - "How do I compile with `-fsanitize`?" |
| 13 | - "My binary is too large / too slow" |
| 14 | - Undefined reference errors, ABI mismatch, missing symbols |
| 15 | |
| 16 | ## Workflow |
| 17 | |
| 18 | ### 1. Choose a build mode |
| 19 | |
| 20 | | Goal | Recommended flags | |
| 21 | |------|-------------------| |
| 22 | | Debug | `-g -O0 -Wall -Wextra` | |
| 23 | | Debug + debuggable optimisation | `-g -Og -Wall -Wextra` | |
| 24 | | Release | `-O2 -DNDEBUG -Wall` | |
| 25 | | Release (max perf, native only) | `-O3 -march=native -DNDEBUG` | |
| 26 | | Release (min size) | `-Os -DNDEBUG` | |
| 27 | | Sanitizer (dev) | `-g -O1 -fsanitize=address,undefined` | |
| 28 | |
| 29 | Always pass `-std=c11` / `-std=c++17` (or the required standard) explicitly. Never rely on the implicit default. |
| 30 | |
| 31 | ### 2. Warning discipline |
| 32 | |
| 33 | Start with `-Wall -Wextra`. For stricter standards compliance add `-Wpedantic`. To treat all warnings as errors in CI: `-Werror`. |
| 34 | |
| 35 | Suppress a specific warning only in a narrow scope: |
| 36 | |
| 37 | ```c |
| 38 | #pragma GCC diagnostic push |
| 39 | #pragma GCC diagnostic ignored "-Wunused-parameter" |
| 40 | // ... |
| 41 | #pragma GCC diagnostic pop |
| 42 | ``` |
| 43 | |
| 44 | Do not pass `-w` (silences everything) except as a last resort for third-party headers. |
| 45 | |
| 46 | ### 3. Debug information |
| 47 | |
| 48 | - `-g` — DWARF debug info, default level 2 |
| 49 | - `-g3` — includes macro definitions (useful with GDB `macro expand`) |
| 50 | - `-ggdb` — DWARF extensions optimal for GDB |
| 51 | - `-gsplit-dwarf` — splits `.dwo` files; reduces link time, needed for `debuginfod` |
| 52 | |
| 53 | Pair `-g` with `-Og` (not `-O0`) when you need readable optimised code in GDB. |
| 54 | |
| 55 | ### 4. Optimisation decision tree |
| 56 | |
| 57 | ```text |
| 58 | Need max throughput on a fixed machine? |
| 59 | yes -> -O3 -march=native -flto |
| 60 | no -> profiling available? |
| 61 | yes -> -O2 -fprofile-use |
| 62 | no -> -O2 |
| 63 | Size-constrained (embedded, shared lib)? |
| 64 | yes -> -Os (or -Oz with clang) |
| 65 | ``` |
| 66 | |
| 67 | `-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. |
| 68 | |
| 69 | `-Ofast`: enables `-ffast-math` which breaks IEEE 754 semantics (NaN handling, associativity). Avoid unless the numerical domain explicitly permits it. |
| 70 | |
| 71 | ### 5. Link-time optimisation (LTO) |
| 72 | |
| 73 | ```bash |
| 74 | # Compile |
| 75 | gcc -O2 -flto -c foo.c -o foo.o |
| 76 | gcc -O2 -flto -c bar.c -o bar.o |
| 77 | # Link (must pass -flto again) |
| 78 | gcc -O2 -flto foo.o bar.o -o prog |
| 79 | ``` |
| 80 | |
| 81 | Use `gcc-ar` / `gcc-ranlib` instead of `ar` / `ranlib` when archiving LTO objects into static libs. |
| 82 | |
| 83 | For parallel LTO: `-flto=auto` (uses `make`-style jobserver) or `-flto=N`. |
| 84 | |
| 85 | See [references/flags.md](references/flags.md) for full flag reference. See `skills/binaries/linkers-lto` for linker-level LTO configuration. |
| 86 | |
| 87 | ### 6. Profile-guided optimisation (PGO) |
| 88 | |
| 89 | ```bash |
| 90 | # Step 1: instrument |
| 91 | gcc -O2 -fprofile-generate prog.c -o prog_inst |
| 92 | # Step 2: run with representative workload |
| 93 | ./prog_inst < workload.input |
| 94 | # Step 3: optimise with profile |
| 95 | gcc -O2 -fprofile-use -fprofile-correction prog.c -o prog |
| 96 | ``` |
| 97 | |
| 98 | `-fprofile-correction` handles profile data inconsistencies from multi-threaded runs. |
| 99 | |
| 100 | ### 7. Preprocessor and standards |
| 101 | |
| 102 | - Inspect macro expansion: `gcc -E file.c | less` |
| 103 | - Dump predefined macros: `gcc -dM -E - < /dev/null` |
| 104 | - Force strict standard: `-std=c11 -pedantic-errors` |
| 105 | - Disable GNU extensions: `-std=c11` (not `-std=gnu11`) |
| 106 | |
| 107 | ### 8. Common error triage |
| 108 | |
| 109 | | Symptom | Likely cause | Fix | |
| 110 | |---------|-------------|-----| |
| 111 | | `undefined reference to 'foo'` | Missing `-lfoo` or wrong link order | Add `-lfoo`; move `-l` flags after object files | |
| 112 | | `multiple definition of 'x'` | Variable defined (not just declared) in a header | Add `extern` in header, define in one `.c` | |
| 113 | | `implicit declaration of function` | Missing `#include` | Add the header | |
| 114 | | `warning: incompatible pointer types` | Wrong cast or missing prototype | Fix the type; check headers | |
| 115 | | ABI errors with C++ | Mixed `-std=` or different `libstdc++` | Unify `-std=` across all TUs | |
| 116 | | `relocation truncated` | Overflow on a 32-bit relative relocation | Use `-mcmodel=large` or restructure code | |
| 117 | |
| 118 | For sanitizer reports, use `skills/runtimes/sanitizers`. |
| 119 | |
| 120 | ### 9. Useful one-liners |
| 121 | |
| 122 | ```bash |
| 123 | # Show all flags enabled at -O2 |
| 124 | gcc -Q --help=optimizers -O2 | grep enabled |
| 125 | |
| 126 | # Preprocess only (che |