Lesson 3 of 5
gcc Compiler Flags - The Production Set
Compiler flags control warnings, optimisation, debugging, standards compliance, and sanitizers. Using just `gcc file.c` is acceptable for quick testing but not for professional code. The right flags catch real bugs at compile time and enable tools like `gdb` and `valgrind`. Know what each flag does - not just that you should use `-Wall`.
Essential gcc Flags - What Each One Does
# ── Warning flags ──────────────────────────────────────────
# -Wall Enable most useful warnings (not literally all warnings)
# Catches: missing return, unused vars, implicit-int, etc.
# -Wextra Enable extra warnings not in -Wall:
# uninitialized vars, sign comparison, unused params
# -Wpedantic Enforce strict ISO C - reject non-standard extensions
# -Wshadow Warn when a local variable shadows a global/outer one
# -Wconversion Warn on implicit type narrowing (e.g. double->int)
# -Wsign-compare Warn when comparing signed and unsigned (included in -Wextra)
# -Wwrite-strings Make string literal type 'const char*' (enforces const)
# ── Standards compliance ─────────────────────────────────────
# -std=c99 C99 (adds for-loop init, bool, variadic macros, stdint.h)
# -std=c11 C11 (adds _Atomic, _Static_assert, aligned_alloc)
# -std=c17 C17 (latest minor revision, defect fixes)
# ── Debugging ────────────────────────────────────────────────
# -g Include debug symbols (line numbers, variable names for gdb)
# -g3 Maximum debug info (includes macro definitions)
# -O0 No optimisation - best for debugging (default with -g)
# -Og Optimise for debugging experience (better than -O0 + readable)
# ── Optimisation ─────────────────────────────────────────────
# -O1 Basic optimisation (faster, smaller, still debuggable)
# -O2 Recommended for production (loop unrolling, inlining)
# -O3 Aggressive (auto-vectorisation - may reorder memory ops)
# -Os Optimise for binary size (embedded systems)
# -march=native Use CPU features of the build machine (SSE, AVX, etc.)
# ── Sanitizers ──────────────────────────────────────────────
# -fsanitize=address AddressSanitizer (ASAN) - heap/stack/global overflows
# -fsanitize=undefined UBSan - undefined behaviour detection
# -fsanitize=address,undefined Combined - use during development ALWAYS
# -fno-omit-frame-pointer Keep frame pointers for better stack traces
# ── Linker flags ─────────────────────────────────────────────
# -lm Link math library (for sqrt, sin, etc.)
# -lpthread Link POSIX threads
# -static Produce statically-linked binary (includes all libs)
# ── Recommended dev command ──────────────────────────────────
# gcc -Wall -Wextra -Wpedantic -std=c99 -g -O0 # -fsanitize=address,undefined # -fno-omit-frame-pointer # source.c -o program
# ── Recommended release command ──────────────────────────────
# gcc -Wall -Wextra -std=c99 -O2 -DNDEBUG source.c -o program
# ── Compile to object file only ─────────────────────────────
# gcc -Wall -std=c99 -c source.c -o source.o
# ── Inspect preprocessed output ─────────────────────────────
# gcc -E source.c | head -50 (see what macros expand to)
# ── Inspect assembly output ─────────────────────────────────
# gcc -S -O2 source.c -o source.s && cat source.sCommon Mistakes
- `-Wall` is NOT all warnings - despite the name, `-Wall` enables only a subset. Critical warnings like `-Wshadow` and `-Wconversion` require separate flags. Use `-Wall -Wextra -Wpedantic` as a baseline.
- Mixing `-g` with `-O2` silently - `-g -O2` is common in production (allows gdb to attach) but optimisation reorders instructions so debugger step-through is jumpy. Use `-g -O0` for pure debugging.
- Not using `-DNDEBUG` in release builds - `assert()` calls remain active without it. In production, disable assertions: `gcc -O2 -DNDEBUG` makes all `assert(...)` compile to nothing.
Tip
Tip
Practice gcc Compiler Flags The Production Set in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Production compiler flags catch subtle bugs at compile time and optimize binary execution
Practice Task
Note
Practice Task - (1) Write a working example of gcc Compiler Flags The Production Set from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with gcc Compiler Flags The Production Set is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.
Key Takeaways
- Compiler flags control warnings, optimisation, debugging, standards compliance, and sanitizers.
- `-Wall` is NOT all warnings - despite the name, `-Wall` enables only a subset. Critical warnings like `-Wshadow` and `-Wconversion` require separate flags. Use `-Wall -Wextra -Wpedantic` as a baseline.
- Mixing `-g` with `-O2` silently - `-g -O2` is common in production (allows gdb to attach) but optimisation reorders instructions so debugger step-through is jumpy. Use `-g -O0` for pure debugging.
- Not using `-DNDEBUG` in release builds - `assert()` calls remain active without it. In production, disable assertions: `gcc -O2 -DNDEBUG` makes all `assert(...)` compile to nothing.
Finished this lesson?