Docs
compilation process
š§ The C++ Compilation Process
Overview
Unlike interpreted languages (Python, JavaScript), C++ is a compiled language. Your source code must be transformed into machine code before it can run.
The Four Stages of Compilation
āāāāāāāāāāāāāāāāāāā
ā Source Code ā (.cpp, .h files)
ā main.cpp ā
āāāāāāāāāā¬āāāāāāāāā
ā STAGE 1: Preprocessing
ā¼
āāāāāāāāāāāāāāāāāāā
ā Preprocessed ā (Expanded code)
ā Code ā
āāāāāāāāāā¬āāāāāāāāā
ā STAGE 2: Compilation
ā¼
āāāāāāāāāāāāāāāāāāā
ā Assembly Code ā (.s file)
āāāāāāāāāā¬āāāāāāāāā
ā STAGE 3: Assembly
ā¼
āāāāāāāāāāāāāāāāāāā
ā Object Code ā (.o file)
āāāāāāāāāā¬āāāāāāāāā
ā STAGE 4: Linking
ā¼
āāāāāāāāāāāāāāāāāāā
ā Executable ā (Binary)
ā ./program ā
āāāāāāāāāāāāāāāāāāā
Stage 1: Preprocessing
The preprocessor handles directives starting with #:
// Before preprocessing
#include <iostream>
#define PI 3.14159
#define SQUARE(x) ((x) * (x))
int main() {
double area = PI * SQUARE(5);
return 0;
}
// After preprocessing
// (iostream contents expanded here - thousands of lines)
int main() {
double area = 3.14159 * ((5) * (5));
return 0;
}
Preprocessor Tasks:
- ā¢#include - Insert contents of header files
- ā¢#define - Replace macros with their values
- ā¢#ifdef/#ifndef - Conditional compilation
- ā¢#pragma - Compiler-specific instructions
- ā¢Remove comments
See Preprocessed Output:
g++ -E main.cpp -o main.i
Stage 2: Compilation
The compiler translates preprocessed code into assembly language:
; Assembly output (simplified)
main:
push rbp
mov rbp, rsp
movsd xmm0, QWORD PTR .LC0[rip]
movsd QWORD PTR [rbp-8], xmm0
mov eax, 0
pop rbp
ret
.LC0:
.long 1374389535
.long 1075388088
What Happens:
- ā¢Syntax checking
- ā¢Type checking
- ā¢Optimization (if enabled)
- ā¢Generate assembly code
See Assembly Output:
g++ -S main.cpp -o main.s
Stage 3: Assembly
The assembler converts assembly code into machine code (object file):
Object file contains:
- Machine code (binary)
- Symbol table (function/variable names)
- Relocation information
Generate Object File:
g++ -c main.cpp -o main.o
Stage 4: Linking
The linker combines object files and libraries into the final executable:
āāāāāāāāāāāāāāāā āāāāāāāāāāāāāāāā
ā main.o ā ā utils.o ā
āāāāāāāā¬āāāāāāāā āāāāāāāā¬āāāāāāāā
ā ā
āāāāāāāāāā¬āāāāāāāāāāāā
ā
āāāāāāāāā¼āāāāāāāā
ā LINKER ā
āāāāāāāāā¬āāāāāāāā
ā
āāāāāāāāāā“āāāāāāāāā
ā ā
āāāāāāāā¼āāāāāāā āāāāāāāā¼āāāāāāā
ā C++ Std ā ā System ā
ā Library ā ā Libraries ā
āāāāāāāāāāāāāāā āāāāāāāāāāāāāāā
ā
āāāāāāāāā¼āāāāāāāā
ā Executable ā
ā ./program ā
āāāāāāāāāāāāāāāāā
Linker Tasks:
- ā¢Resolve external references
- ā¢Combine object files
- ā¢Link standard library functions
- ā¢Create final executable
Complete Compilation Commands
# All-in-one (most common)
g++ main.cpp -o program
# Step by step
g++ -E main.cpp -o main.i # Preprocess
g++ -S main.i -o main.s # Compile to assembly
g++ -c main.s -o main.o # Assemble to object
g++ main.o -o program # Link to executable
# With multiple files
g++ file1.cpp file2.cpp file3.cpp -o program
# With options
g++ -std=c++17 -Wall -Wextra -O2 main.cpp -o program
Compiler Flags Explained
| Flag | Purpose |
|---|---|
-std=c++17 | Use C++17 standard |
-Wall | Enable common warnings |
-Wextra | Enable extra warnings |
-Werror | Treat warnings as errors |
-O0 | No optimization (debug) |
-O2 | Moderate optimization |
-O3 | Aggressive optimization |
-g | Include debug symbols |
-c | Compile only (no linking) |
-o name | Specify output filename |
Common Compilation Errors
1. Syntax Error (Compilation Stage)
int main() {
cout << "Hello" // Missing semicolon
return 0;
}
error: expected ';' before 'return'
2. Undefined Reference (Linking Stage)
void myFunction(); // Declared but never defined
int main() {
myFunction(); // Linker can't find it
return 0;
}
undefined reference to 'myFunction()'
3. Multiple Definition (Linking Stage)
// file1.cpp
int global = 5;
// file2.cpp
int global = 10; // Same name!
multiple definition of 'global'
Practical Exercise
Create a simple program and observe the compilation stages:
// hello.cpp
#include <iostream>
int main() {
std::cout << "Hello, C++ World!" << std::endl;
return 0;
}
# Try each stage
g++ -E hello.cpp > hello.i # Look at preprocessed output
g++ -S hello.cpp # Creates hello.s (assembly)
g++ -c hello.cpp # Creates hello.o (object)
g++ hello.o -o hello # Creates executable
./hello # Run it!
Next: Your First C++ Program