C Tutorial

Compilation Process

Preprocessing, compiling, assembling, and linking — how source becomes executable

The Journey from Source to Executable

When you write a C program and hit "compile," a remarkable multi-stage pipeline transforms your human-readable code into machine instructions. Understanding this pipeline is essential for debugging, optimizing, and truly mastering C.
Key Insight: Unlike interpreted languages (Python, JavaScript), C compiles directly to native machine code — which is why it's so fast.

The Four Stages

The compilation process has four distinct stages:
  1. Preprocessing (cpp)
  2. Compilation (cc1)
  3. Assembly (as)
  4. Linking (ld)
Let's walk through each one.

Stage 1: Preprocessing

The preprocessor handles all lines beginning with #. It performs textual substitution before the compiler ever sees your code.
c
// Before preprocessing
#include <stdio.h>
#define MAX_SIZE 100
#define SQUARE(x) ((x) * (x))

int main() {
    int arr[MAX_SIZE];
    int result = SQUARE(5);
    printf("Result: %d\n", result);
    return 0;
}
What the preprocessor does:
  • Replaces #include directives with the actual file contents
  • Expands #define macros (e.g., MAX_SIZE → 100)
  • Evaluates #ifdef / #ifndef conditional blocks
  • Strips out comments
You can see the preprocessor output yourself:
bash
gcc -E program.c -o program.i
The resulting .i file can be thousands of lines long because it includes everything from stdio.h and its dependencies.

Stage 2: Compilation

The compiler takes the preprocessed source and converts it into assembly language — a human-readable representation of machine instructions specific to your CPU architecture.
bash
gcc -S program.c -o program.s
A simple printf("Hello") might produce assembly like:
asm
    .section .rodata
.LC0:
    .string "Hello"
    .text
    .globl main
main:
    pushq   %rbp
    movq    %rsp, %rbp
    leaq    .LC0(%rip), %rdi
    call    printf@PLT
    movl    $0, %eax
    popq    %rbp
    ret
This is where syntax errors, type errors, and warnings are caught. The compiler also performs optimizations at this stage (when you use flags like -O2 or -O3).

Stage 3: Assembly

The assembler converts assembly language into object code — raw binary machine instructions. The output is a .o file.
bash
gcc -c program.c -o program.o
Object files contain:
  • Machine code for your functions
  • A symbol table listing functions and variables
  • Relocation entries — placeholders for addresses not yet known
Note: Object files are not executable yet. They contain unresolved references (like calls to printf) that need to be connected to their actual implementations.

Stage 4: Linking

The linker combines one or more object files with library code to produce the final executable.
bash
gcc program.o -o program
The linker resolves all those unresolved references:
  • Connects your printf call to the C standard library (libc)
  • Merges multiple .o files from a multi-file project
  • Sets up the program's entry point
There are two types of linking:
  • Static linking — library code is copied into the executable
  • Dynamic linking — the executable references shared libraries (.so / .dylib) loaded at runtime

The Complete Pipeline

Here's the full command that runs all four stages:
bash
# All-in-one (most common)
gcc program.c -o program

# Equivalent to running each stage manually:
gcc -E program.c -o program.i    # Preprocess
gcc -S program.i -o program.s    # Compile to assembly
gcc -c program.s -o program.o    # Assemble to object code
gcc    program.o -o program      # Link to executable

Useful Compiler Flags

bash
# Show all warnings (you should ALWAYS use this)
gcc -Wall -Wextra -Werror program.c -o program

# Optimization levels
gcc -O0 program.c -o program     # No optimization (default, best for debugging)
gcc -O2 program.c -o program     # Recommended for production
gcc -O3 program.c -o program     # Aggressive optimization

# Generate debug information (for gdb)
gcc -g program.c -o program

# Specify the C standard
gcc -std=c11 program.c -o program
gcc -std=c17 program.c -o program
Best Practice: Always compile with -Wall -Wextra during development. These flags catch subtle bugs that would otherwise go unnoticed.

Common Compilation Errors

ErrorMeaning
undefined reference to 'foo'Linker can't find the function — missing .o file or library
implicit declaration of functionMissing #include or prototype
expected ';' before '}'Syntax error — missing semicolon
incompatible pointer typeType mismatch in pointer assignment

Summary

Understanding the compilation pipeline gives you a significant advantage:
  • Preprocessing lets you write flexible, configurable code
  • Compilation is where you catch bugs early with warnings
  • Assembly shows you exactly what the CPU will execute
  • Linking connects your code to the outside world
When something goes wrong, knowing which stage failed tells you exactly where to look.