Reading history

C Compilation Process: Preprocess, Compile, Assemble, Link

10 min read 8 views

See what happens when you run gcc: preprocessing, compiling, assembling and linking, with the commands to stop at each stage and inspect the output files.

Turning a .c file into a program takes four separate steps, even though gcc hides them behind one command. Seeing each stage makes compiler and linker errors much easier to understand.


What Happens When You Run gcc?

What is Compilation?

Compilation is the sequence of steps that converts a C source file (.c) into an executable program or a library. Unlike interpreted languages, C code is not executed line-by-line; instead, the entire source is processed before execution. The standard compilation process involves four distinct phases:

  1. Preprocessing
  2. Compilation (proper)
  3. Assembly
  4. Linking

Each phase produces an intermediate file that serves as input to the next phase. While most programmers use a single command (gcc file.c -o program) to generate the executable, understanding the individual steps allows for finer control, debugging, and optimization.

Why Understand the Compilation Process?

  • Debugging: Distinguish between compile-time errors (syntax, type) and link-time errors (missing symbols, undefined references).
  • Optimization: Know where to apply compiler flags (e.g., -O2, -flto) for performance gains.
  • Build Systems: Understand how object files and libraries combine, which is fundamental for Makefiles and large projects.
  • Cross-Compilation: Target different architectures by controlling the assembly and linking stages.
  • Low-Level Insight: See how high-level constructs map to assembly language and machine code.

The Four Phases at a Glance

PhaseInputOutputKey Actions
Preprocessing.c source.i preprocessedRemove comments, expand macros, include headers
Compilation.i preprocessed.s assemblyTranslate C to assembly language
Assembly.s assembly.o objectConvert assembly to machine code (relocatable)
Linking.o object(s) + librariesexecutableResolve symbols, combine object files, add startup code

The Four Stages and Their Output Files

Step 1: Creating a Source File

We begin with a simple C program saved as hello.c:

/*
 * hello.c
 * A simple C program demonstrating compilation phases.
 */

#include <stdio.h>   // Preprocessor: includes standard I/O declarations

#define GREETING "Hello, World!\n"   // Macro definition

int main(void) {
    printf(GREETING);  // Macro will be expanded
    return 0;
}

Note: Comments, preprocessor directives (#include, #define), and the macro GREETING will all be processed during preprocessing.

Step 2: Preprocessing (gcc -E)

The preprocessor handles directives that start with #. It produces a translation unit with all headers expanded, macros replaced, and comments stripped.

Command:

gcc -E hello.c -o hello.i

Result: The file hello.i contains the expanded code. It will be much larger because stdio.h includes many declarations. At the end, you’ll find the original code with printf(GREETING); replaced by printf("Hello, World!\n");.

Key actions of the preprocessor:

  • Remove comments: All /* ... */ and // ... are deleted.
  • Expand macros: #define constants and function-like macros are substituted.
  • Include headers: The entire contents of #include files are inserted.
  • Conditional compilation: #ifdef, #ifndef, #endif directives are evaluated.
  • Line control: Adds #line directives to help compilers report original line numbers.

Step 3: Compilation Proper (gcc -S)

The compiler translates the preprocessed C code into assembly language specific to the target architecture.

Command:

gcc -S hello.i -o hello.s

This produces an assembly file hello.s. Example snippet (x86-64):

asm

    .section    __TEXT,__text,regular,pure_instructions
    .globl  _main
_main:
    pushq   %rbp
    movq    %rsp, %rbp
    leaq    L_.str(%rip), %rdi
    callq   _printf
    xorl    %eax, %eax
    popq    %rbp
    retq
L_.str:
    .asciz  "Hello, World!\n"

Key points:

  • The assembly code is human-readable but low-level.
  • It includes labels, registers, and instructions specific to the CPU.
  • Function calls (like printf) are still symbolic; their addresses are not yet resolved.

Step 4: Assembly (gcc -c)

The assembler converts assembly code into machine code, producing an object file (.o). This file contains binary instructions and data, but symbol references (like printf) remain unresolved.

Command:

gcc -c hello.s -o hello.o

The object file hello.o is not directly executable. It is in Executable and Linkable Format (ELF) on Linux or Mach-O on macOS, containing:

  • Machine code for each function
  • A symbol table listing defined and undefined symbols
  • Relocation information for unresolved references
  • Debugging information (if compiled with -g)

You can inspect the object file with nm or objdump:

nm hello.o

Output (partial):

                 U _printf
0000000000000000 T _main

Here U means undefined (needs to be linked), and T means defined in the text section.

Step 5: Linking (gcc)

The linker combines one or more object files and libraries to produce the final executable. It resolves all undefined symbols by finding their definitions in other object files or libraries.

Command:

gcc hello.o -o hello

This invokes the linker (usually ld internally). The linker performs:

  • Symbol resolution: Finds the address of printf in the C standard library (libc). By default, GCC links dynamically, so printf is referenced from a shared library (libc.so or libc.dylib).
  • Relocation: Adjusts addresses in the object code to final memory locations.
  • Adding startup code: Inserts crt0.o and related files that set up the environment (stack, command-line arguments) before calling main.
  • Combining sections: Merges text, data, and BSS sections from multiple object files.

Result: The executable hello is produced. You can run it:

./hello

Output:

Hello, World!

Static vs. Dynamic Linking

  • Static Linking: All library code is copied into the executable. The binary is self-contained but larger. Use gcc -static hello.o -o hello_static.
  • Dynamic Linking (default): The executable contains references to shared libraries. At runtime, the dynamic linker loads the required libraries. Smaller binaries, but dependent on library availability.

Check linking type with ldd (Linux) or otool -L (macOS).


Watching Each Stage With gcc Options

We now demonstrate the complete compilation process on a more complex program that uses multiple source files, a custom header, and a function call. This illustrates how the phases interact across modules.

Program Structure

  • main.c – Contains the main function.
  • mathutils.c – Implements a utility function.
  • mathutils.h – Declares the utility function and a macro.

Source Files

mathutils.h:

#ifndef MATHUTILS_H
#define MATHUTILS_H

#define SQUARE(x) ((x) * (x))

int add(int a, int b);

#endif

mathutils.c:

#include "mathutils.h"

int add(int a, int b) {
    return a + b;
}

main.c:

#include <stdio.h>
#include "mathutils.h"

int main(void) {
    int x = 5, y = 3;
    int sum = add(x, y);
    int square = SQUARE(x);
    printf("Sum: %d, Square: %d\n", sum, square);
    return 0;
}

Step-by-Step Compilation

1. Preprocess each source file

gcc -E main.c -o main.i
gcc -E mathutils.c -o mathutils.i

In main.i, the macro SQUARE(x) is expanded to ((x) * (x)), and the contents of mathutils.h and stdio.h are inserted.

2. Compile each preprocessed file to assembly

gcc -S main.i -o main.s
gcc -S mathutils.i -o mathutils.s

Each .s file contains assembly for the corresponding translation unit.

3. Assemble each assembly file to object code

gcc -c main.s -o main.o
gcc -c mathutils.s -o mathutils.o

At this point, main.o contains an undefined reference to add, and mathutils.o defines add.

4. Link the object files into an executable

gcc main.o mathutils.o -o program

The linker resolves the reference to add and links the C standard library for printf. The final executable is program.

Run the program

./program

Expected output:

Sum: 8, Square: 25

Using a Single Command (with intermediate files)

To automatically save intermediate files, use:

gcc -Wall -save-temps main.c mathutils.c -o program

This generates main.i, main.s, main.o, and similarly for mathutils.c, plus the executable.

Explanation of Key Concepts

  • Header Guards: #ifndef MATHUTILS_H prevents multiple inclusion, a common preprocessor feature.
  • Macro Expansion: SQUARE(x) becomes ((x) * (x)) before compilation.
  • Separate Compilation: Each .c file is compiled independently; the linker combines them. This is the foundation of modular programming.
  • Symbol Resolution: The linker ensures that every function call has a definition somewhere.

Compiler vs Linker Errors

Understanding the compilation pipeline helps avoid common errors. Below are frequent pitfalls and how to address them.

1. Preprocessor Errors

  • Missing Header Files: If #include "mathutils.h" fails, the preprocessor emits fatal error: mathutils.h: No such file or directory. Ensure the header is in the include path (-I flag).
  • Macro Pitfalls: Macros are textual substitutions; missing parentheses can cause unexpected precedence. Always parenthesize macro parameters and the whole expression.c#define SQUARE(x) x * x // BUG: SQUARE(2+3) becomes 2+3*2+3 = 11
  • Unterminated Comments: Forgetting to close /* causes the rest of the file to be treated as comment.

2. Compilation Errors

  • Syntax Errors: Missing semicolons, unmatched braces, etc. The compiler reports line numbers, but after preprocessing, the line numbers may refer to the .i file unless #line directives are used (they usually are).
  • Type Errors: Assigning incompatible types, using undeclared variables. Enable warnings with -Wall -Wextra to catch many.
  • Undefined Behavior in Code: Some constructs may compile but cause runtime errors. Use -fsanitize=undefined to detect.

3. Assembly Errors

Rare, but can occur if the compiler generates invalid assembly for the target architecture (e.g., incompatible CPU features). Use -march to specify target.

4. Linking Errors

  • Undefined Reference: If a function is called but not defined or not linked, the linker reports undefined reference to 'foo'. Fix by providing the object file or library containing foo.
  • Multiple Definition: If the same global symbol is defined in two object files, the linker reports multiple definition of 'foo'. Use static for file-scope functions or avoid duplicate definitions.
  • Library Order: When linking static libraries, order matters. Place libraries after object files that reference them: gcc main.o -lm (math library) not gcc -lm main.o.
  • Missing Libraries: If using functions from libm (e.g., sqrt), you must link with -lm. Failure results in undefined reference.

5. Runtime Dynamic Linking Issues

  • Shared Library Not Found: If the executable depends on a shared library not in the system path, running it fails with error while loading shared libraries. Set LD_LIBRARY_PATH (Linux) or install the library.
  • Version Mismatch: If the library version differs from what was used at compile time, the program may crash.

6. Memory Considerations

  • Object File Size vs. Executable Size: Object files are smaller; the linker adds startup code and library stubs, increasing size. Static linking dramatically increases executable size because entire library code is copied.
  • Debug Information: Compiling with -g increases object and executable size due to debug symbols. Strip with strip command for release.
  • Link-Time Optimization (LTO): Using -flto allows the compiler to optimize across translation units, but increases link time and memory usage.

7. Compiler Flags and Optimization

  • -O0 (default): No optimization, faster compilation.
  • -O2, -O3: High optimization, may increase compile time and binary size but improve runtime speed.
  • -Os: Optimize for size.
  • -march=native: Generate code for the current CPU, possibly using advanced instructions; not portable.

Compilation Stages at a Glance

The C compilation process is a four-stage pipeline: preprocessing → compilation → assembly → linking. Each stage transforms the source into a more machine-oriented representation, culminating in an executable. Understanding these stages is fundamental for debugging, optimizing, and building complex software.

Key Takeaways

  • Preprocessing handles #include, #define, comments, and conditional compilation.
  • Compilation translates C to assembly; assembly converts assembly to object code.
  • Linking resolves symbols and combines object files and libraries into an executable.
  • Static linking copies library code; dynamic linking references shared libraries.
  • Intermediate files (.i, .s, .o) can be inspected to diagnose issues.

Best Practices

  1. Use -Wall -Wextra -pedantic to enable all warnings and catch potential bugs early.
  2. Separate compilation units for modularity and faster incremental builds.
  3. Always use header guards to prevent multiple inclusion.
  4. Parenthesize macro parameters and expressions to avoid precedence issues.
  5. Check linker errors carefully – they often point to missing implementations or incorrect library order.
  6. Prefer dynamic linking for smaller binaries unless distribution requires static linking.
  7. Use -save-temps to examine intermediate files when debugging preprocessor or assembly issues.
  8. Optimize appropriately: Use -O2 for release, but avoid over-optimization that may introduce subtle bugs (e.g., relying on undefined behavior).
  9. Understand your toolchain: Know how to invoke the preprocessor, compiler, assembler, and linker separately.
  10. For large projects, use a build system like Make or CMake to manage compilation and dependencies.

Mastering the compilation process not only makes you a better C programmer but also demystifies how software runs on real hardware.

Lessons are also marked complete when you click Next.