Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 2 of 10

The Compilation Pipeline - Source to Executable

When you run `gcc hello.c -o hello`, four distinct programs execute in sequence: the preprocessor, the compiler, the assembler, and the linker. Each transforms your source code through a different representation. Understanding this pipeline explains why `#include` is not an import, why header files exist, and where 'undefined reference' errors come from.

13 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

The Four Stages - What Each Tool Does

The Four Stages - What Each Tool Does
/* Stage 1: Preprocessor (cpp)
   Input:  hello.c
   Output: hello.i (expanded source)
   Action: Processes all directives starting with #
           - #include → pastes the contents of the file
           - #define  → textual substitution
           - #ifdef   → conditional code removal          */

// Run the preprocessor only:
// gcc -E hello.c -o hello.i

/* Stage 2: Compiler (cc1)
   Input:  hello.i (preprocessed C)
   Output: hello.s (assembly language)
   Action: Parses C, checks types, generates assembly.
           THIS is where type errors are caught.         */

// Run up to compilation only:
// gcc -S hello.i -o hello.s

/* Stage 3: Assembler (as)
   Input:  hello.s (assembly)
   Output: hello.o (object file - machine code + metadata)
   Action: Converts assembly mnemonics to binary opcodes.
           References to external symbols (printf) are
           left unresolved as relocations.               */

// Produces an object file:
// gcc -c hello.c -o hello.o

/* Stage 4: Linker (ld)
   Input:  hello.o + libc.a (C standard library)
   Output: hello (ELF executable)
   Action: Resolves all symbol references.
           printf → address in libc.
           'undefined reference to foo' = linker error,
           not a compiler error.                         */

// Full compilation:
// gcc hello.c -o hello

Anatomy of a Translation Unit

Anatomy of a Translation Unit
/* Each .c file is one "translation unit" - the unit of compilation.
   Header files (.h) are NOT translation units; they are pasted
   into translation units by the preprocessor.                   */

/* hello.c - a minimal but correct C program */
/* 1. System header - pasted by preprocessor */
#include <stdio.h>    /* declares printf, scanf, FILE, etc. */

/* 2. A preprocessor constant - replaced with 42 everywhere */
#define MAX_COUNT 42

/* 3. Function prototype - tells the compiler the function exists */
void print_greeting(const char *name);

/* 4. The entry point - C execution starts here, ALWAYS */
int main(void) {      /* 'void' explicitly says: no arguments */
    print_greeting("World");
    return 0;         /* 0 = success; non-zero = error code */
}

/* 5. Function definition - the actual implementation */
void print_greeting(const char *name) {
    printf("Hello, %s!
", name);
}

/* Build and inspect:
   gcc -Wall -Wextra -std=c99 hello.c -o hello
   ./hello
   echo $?    ← prints 0 (the return value of main)       */

Common Mistakes

  • Confusing a `#include` error with a compile error - `#include <foo.h>` failing means the preprocessor cannot find the file. It runs BEFORE the compiler. It's not a syntax error.
  • 'undefined reference' is a LINKER error, not a compiler error - if you declare `int add(int,int)` in a header but forget to compile the `.c` file that defines it, the compiler is happy but the linker fails.
  • Compiling without `-Wall -Wextra` - the default gcc mode suppresses many real bugs as warnings. Always use `gcc -Wall -Wextra -Wpedantic -std=c99` during development.

Tip

Tip

Practice The Compilation Pipeline Source to Executable in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Four distinct stages transform human-readable C source into machine-executable binaries

Practice Task

Note

Practice Task - (1) Write a working example of The Compilation Pipeline Source to Executable 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 The Compilation Pipeline Source to Executable 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

  • When you run `gcc hello.
  • Confusing a `#include` error with a compile error - `#include <foo.h>` failing means the preprocessor cannot find the file. It runs BEFORE the compiler. It's not a syntax error.
  • 'undefined reference' is a LINKER error, not a compiler error - if you declare `int add(int,int)` in a header but forget to compile the `.c` file that defines it, the compiler is happy but the linker fails.
  • Compiling without `-Wall -Wextra` - the default gcc mode suppresses many real bugs as warnings. Always use `gcc -Wall -Wextra -Wpedantic -std=c99` during development.

Topics in This Module

0/10 done