The Compilation Pipeline
Every C++ program goes through multiple transformation stages before becoming executable machine code. This overview shows the complete pipeline:
C++ Compilation Pipeline
Follow one translation unit from source text to an object file. Select a stage to inspect its input and output.
Source Code: Human-readable C++ code
Source Code
- Input
- main.cpp
- Output
- main.cpp
Illustrative File Sizes
Compilation Stages
Each stage transforms your code closer to machine language:
1. Preprocessing
The preprocessor rewrites the source text before the compiler proper sees it:
- Macro expansion
- Include processing
- Conditional compilation
- Output: Expanded source (.i file)
2. Parsing & AST
The parser turns the token stream into an abstract syntax tree:
- Lexical analysis (tokenization)
- Syntax analysis (parsing)
- Build Abstract Syntax Tree
- Output: AST representation
3. Semantic Analysis
- Type checking
- Name resolution
- Template instantiation
- Output: Annotated AST
4. Optimization
The optimizer rewrites the intermediate representation:
- Code improvements
- Performance enhancements
- Size reductions
- Output: Optimized IR
5. Code Generation
- Target-specific assembly
- Register allocation
- Instruction selection
- Output: Assembly (.s file)
6. Assembly
- Convert to machine code
- Create object file
- Output: Object file (.o)
Quick Commands
# Complete compilation
g++ main.cpp -o program
# Stop after preprocessing
g++ -E main.cpp -o main.i
# Generate assembly
g++ -S main.cpp -o main.s
# Create object file only
g++ -c main.cpp -o main.o
# With optimization
g++ -O2 main.cpp -o program
Compilation Deep Dives
Explore each stage in detail:
- Preprocessor Directives - Macros and includes
- AST & Parsing - Code structure analysis
- Optimization Techniques - Performance improvements
- Object Files - Binary format and symbols
What's Next?
Object Files Need Linking and Loading
After compilation, your object files need to be:
- Linked together to resolve symbols
- Loaded into memory for execution
Related Topics
Related concepts
Follow a C++ function from tokens to a simplified AST, then inspect exact source ranges, declaration references and types in a keyboard-accessible explorer.
C++ preprocessor visualized: macros, header guards, conditional compilation, and #include directives explained interactively.
Master C++ OOP concepts including inheritance, polymorphism, virtual functions, and modern object-oriented design principles with interactive examples.
How C++ object files are linked into executables. Learn symbol resolution, static vs dynamic linking, and linker optimization.
C++ compiler optimization lab notebook — compare optimization levels, inspect compiler rewrites, diagnose auto-vectorization, and build production verification commands.
Understand C++ pointers and references with step-by-step memory diagrams: addressing, dereferencing, aliasing, array arithmetic, and object lifetime.
