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

Compilation Pipeline - preprocessor, compiler, assembler, linker

C++ source code goes through **four stages** before becoming an executable. Understanding this pipeline is essential for debugging build errors and understanding why certain things (like template definitions in headers, `inline`, `#include`) work the way they do.

12 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Four-Stage Compilation - With Commands

Four-Stage Compilation - With Commands
// === main.cpp ===
#include <iostream>   // preprocessor: copy iostream content here

int main() {
    std::cout << "Hello, C++17!
";
    return 0;
}

// ─── Stage 1: Preprocessor ─────────────────────────────────
// cpp main.cpp -o main.i
// Handles: #include, #define, #ifdef, #pragma
// Output: one giant .i file with everything expanded

// ─── Stage 2: Compiler ─────────────────────────────────────
// g++ -std=c++17 -S main.i -o main.s
// Parses C++, type-checks, generates assembly code (.s)
// This is where: template instantiation, overload resolution happen

// ─── Stage 3: Assembler ────────────────────────────────────
// as main.s -o main.o
// Converts assembly to machine code - object file (.o)
// Object file has unresolved symbols (e.g., std::cout)

// ─── Stage 4: Linker ───────────────────────────────────────
// g++ main.o -o main    (links libstdc++ automatically)
// Resolves external symbols, combines .o files into executable
// "undefined reference to" errors = linker errors

// ─── All in one command (practical) ────────────────────────
// g++ -Wall -Wextra -std=c++17 -O2 main.cpp -o main

// ─── Separate compilation (multi-file project) ─────────────
// g++ -std=c++17 -c math.cpp  -o math.o   (compile only)
// g++ -std=c++17 -c main.cpp  -o main.o
// g++ math.o main.o -o app              (link)

// ─── Key flags ─────────────────────────────────────────────
// -Wall -Wextra         Enable most useful warnings
// -Wpedantic            Strict ISO C++ conformance
// -std=c++17            Use C++17 standard
// -O0                   No optimisation (debug)
// -O2                   Production optimisation level
// -g                    Debug symbols (for gdb)
// -fsanitize=address,undefined   ASAN + UBSan (dev/test)
// -o <name>             Output file name

Common Mistakes

  • Forgetting `-std=c++17` - you'll get 'auto not allowed in C++98' or 'structured bindings require C++17' errors that are confusing without understanding the flag.
  • 'undefined reference' vs 'error: use of undeclared identifier' - the first is a **linker** error (symbol exists but .o not included in link). The second is a **compiler** error (not even declared in scope).
  • Putting function definitions in headers without `inline` - if two .cpp files include the same header with a non-inline function definition, you get 'multiple definition of ...' linker error. Either mark it `inline` or move definition to a .cpp file.

Tip

Tip

Practice Compilation Pipeline preprocessor compiler assembler linker in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Preprocessor -> Compiler -> Linker compilation pipeline

Practice Task

Note

Practice Task - (1) Write a working example of Compilation Pipeline preprocessor compiler assembler linker 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 Compilation Pipeline preprocessor compiler assembler linker is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cpp code.

Key Takeaways

  • C++ source code goes through **four stages** before becoming an executable.
  • Forgetting `-std=c++17` - you'll get 'auto not allowed in C++98' or 'structured bindings require C++17' errors that are confusing without understanding the flag.
  • 'undefined reference' vs 'error: use of undeclared identifier' - the first is a **linker** error (symbol exists but .o not included in link). The second is a **compiler** error (not even declared in scope).
  • Putting function definitions in headers without `inline` - if two .cpp files include the same header with a non-inline function definition, you get 'multiple definition of ...' linker error. Either mark it `inline` or move definition to a .cpp file.

Topics in This Module

0/10 done