skillfed

gcc

Master GCC flag selection for debug and release builds, from optimization levels and warning discipline to link-time optimization and profile-guided optimization. Covers common compilation errors, sanitizer instrumentation, and integration with GNU Make and CMake.

GCC helps you select compiler flags for debug and release builds, optimization levels, and troubleshoot compilation errors.

AI-generated summary based on this skill's SKILL.md

148 19 MIT updated by mohitmishra786

Install

mohitmishra786/low-level-dev-skills/gcc · repository language: JavaScript

git clone https://github.com/mohitmishra786/low-level-dev-skills
cp -r low-level-dev-skills/skills/compilers/gcc ~/.claude/skills/gcc
npx skillfed install mohitmishra786/low-level-dev-skills/gcc

Frequently asked questions

AI-generated answers based on this skill's SKILL.md and metadata

What are the best GCC optimization flags for a release build?

gcc's release build typically uses -O2 or -O3 for optimization. -O2 balances speed and compile time with good safety; -O3 enables aggressive optimizations but may increase code size and compile time. Pair with -DNDEBUG to disable assertions, -s to strip symbols, and -flto for link-time optimization. For size-constrained builds, use -Os instead. Always profile your specific workload to choose between -O2 and -O3.

How do I enable link time optimization in GCC?

gcc's link-time optimization (LTO) is enabled by adding -flto to both compilation and linking stages. Compile with `gcc -flto -O2 -c file.c` and link with `gcc -flto -O2 file.o -o executable`. LTO allows the compiler to optimize across translation unit boundaries, often reducing binary size and improving performance. Use -flto=auto to parallelize LTO during linking, or -flto=N to use N parallel jobs.

How do I fix a gcc compilation error about undefined reference?

gcc's undefined reference error means a symbol was declared but not defined or linked. Check: (1) all .o files are linked together, (2) required libraries are linked with -lname, (3) symbol names match (case-sensitive, no name mangling mismatches), (4) link order is correct (dependents before dependencies). Use `nm` to inspect object files and verify symbols exist. For C++ code, ensure extern "C" is used for C symbols.

What gcc flags should I use for debug builds?

gcc debug builds use -g to include debugging symbols, -O0 to disable optimization for accurate stepping, and optionally -DDEBUG for conditional debug code. Use -Wall -Wextra for comprehensive warnings. For sanitizers, add -fsanitize=address or -fsanitize=undefined to catch runtime errors. Keep -O0 with sanitizers to avoid optimizations that obscure bugs. Never strip symbols with -s in debug builds.

How do I enable profile-guided optimization with GCC?

gcc's profile-guided optimization (PGO) uses two passes: (1) Compile with -fprofile-generate: `gcc -O2 -fprofile-generate file.c -o prog`, run the program on representative input to generate .gcda files, (2) Recompile with -fprofile-use: `gcc -O2 -fprofile-use file.c -o prog`. PGO lets the compiler optimize based on actual runtime behavior, often yielding 5-15% performance gains over static optimization.

What gcc sanitizer flags are available for runtime safety checks?

gcc's sanitizer flags instrument code to detect runtime errors. -fsanitize=address detects memory errors, -fsanitize=undefined catches undefined behavior, -fsanitize=thread finds data races, -fsanitize=leak detects memory leaks. Combine multiple: -fsanitize=address,undefined. Always compile and link with the same sanitizer flag. Sanitizers add overhead; use only in debug/testing builds. Set ASAN_OPTIONS or UBSAN_OPTIONS environment variables to control behavior.

SKILL.md

rendered from the published skill — quoted content, verbatim

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

(truncated - see the full file via the links below)

Read as markdown · JSON record · Browse the source repository

File tree — 3 files
skills/compilers/gcc/SKILL.md
skills/compilers/gcc/references/examples.md
skills/compilers/gcc/references/flags.md

Related skills

Tags

compiler-optimization build-configuration error-diagnosis performance-tuning memory-safety debug-symbols link-time-optimization warning-management profiling-instrumentation standards-compliance