C Mastery / Debug vs. Release Builds
Part 5 — Compilation, Linking, and Building

Debug vs. Release Builds

This chapter contrasts debug and release builds: their flags, their properties, and why behavior can differ between them.

Why This Matters

"Works in debug, breaks in release" is one of the most common C experiences. The two builds differ in optimization, assertions, and instrumentation, and those differences expose latent undefined behavior.

Prerequisites

Core Concept

Debug build

Goal: easy stepping, predictable execution, clear diagnostics.

Release build

Goal: speed and small size.

Examples

# debug
cc -std=c17 -Wall -Wextra -g -O0 -fsanitize=address,undefined main.c -o app-debug

# release
cc -std=c17 -O2 -DNDEBUG main.c -o app

How It Works

Optimization transforms code aggressively (c.opt.1), which can change timing, reorder operations, and remove "dead" code. NDEBUG removes assert. Sanitizers add runtime checks that slow the program but catch errors. Debug info maps machine code back to source.

Variations

-Og

-Og optimizes just enough to keep debugging pleasant, a middle ground between -O0 and -O2.

Release with symbols

You can keep -g in a release build and strip symbols into a separate file, enabling debugging of optimized code (c.debug.5).

Common Mistakes

Undefined Behavior

broken; the optimizer reveals it.

Portability

universal.

Under the Hood

At -O0, the compiler emits straightforward, often redundant code. At -O2, it applies inlining, dead-code elimination, and other passes that rely on the absence of UB (c.opt.2).

Practical Usage

Exercises

1. Build the same program at -O0 and -O2; compare time and binary size. 2. Use assert and observe it vanish with -DNDEBUG. 3. Find a program that behaves differently at -O2 due to UB (or construct a strict-aliasing example) and fix it.

Deep Challenge

Explain, with a concrete strict-aliasing or signed-overflow example, how a release build can produce output that differs from a debug build, and why the program was always undefined. Show the fix.

References

Verification

verified.`

Progress

Concept checkboxes

Mastery levels

ConceptCurrent level (0–8)Target level
c.build.debug05
c.build.release05