The process of converting human-readable C++ code into an executable machine program involves multiple stages. Each stage plays a critical role in ensuring that our code runs as intended.
We'll walk through the entire C++ compilation process, step-by-step, from source code to executable.
Steps involved in Compilation:
- Source Code (.cpp, .h files)
- Preprocessing
- Compilation
- Assembly
- Linking
- Execution
1. Source Code
Source code is the C++ code that the programmer writes. It usually consists of:
- .cpp files: contain function definitions and main()
- .h files: contain function declarations or constants (headers)
Example:
C++
// main.cpp
#include <iostream>
#include "math_utils.h"
int main() {
int result = add(10, 5);
std::cout << "Result: " << result << std::endl;
return 0;
}
2. Preprocessing
The preprocessor handles lines that start with # (like #include, #define, etc.) and prepares the code for the compiler. It:
- Replaces #include <....> with actual code from the included files.
- Expands macros
- Handles conditional compilation (#ifdef, etc.)
Command:
g++ -E main.cpp -o main.i
Output:
The output (main.i) contains pure C++ code with all header files inserted and macros expanded.
Example:
C++
#include <iostream> // becomes --> contents of <iostream> inserted
#include "math_utils.h" // becomes --> int add(int a, int b);
3. Compilation
The compiler translates preprocessed C++ code into assembly language, which is human readable low-level code, It :
- Does Syntax checking
- Converts C++ to Assembly code (.s file)
Command
g++ -S main.i -o main.s
Output
main.s contains low-level assembly instructions.
Example:
C++
C++
Assembly
C++
movl $10, -4(%rbp)
movl $5, -8(%rbp)
call add
4. Assembly
The assembler converts the assembly code into object code, which is machine-readable binary (.o file).
Command
g++ -c main.cpp -o main.o
Output
A .o (object file) that contains machine code, not yet executable on its own
5. Linking
The linker combines multiple object files (main.o, math_utils.o) and links in external libraries (like iostream), creating a final executable. It:
- Connects the main() function in main.o to add() in math_utils.o
- Resolves symbols from the standard library
Command
g++ main.o math_utils.o -o main
Output
An executable file (usually called main or whatever name you give)
6. Execution
It is the final executable produced by the linker.
Command
./main
Output
Result: 15
Stage | Tool | Example command |
|---|
Preprocessing | g++ -E | g++ -E main.cpp -o main.i |
|---|
Compilation | g++ -S | g++ -S main.i -o main.s |
|---|
Assembly | g++ -c | g++ -c main.s -o main.o |
|---|
Linking | g++ | g++ main.o -o main |
|---|
Automation | make | Automate multi-file builds |
|---|
Common Errors at Each Stage
Stage | Common Errors |
|---|
Preprocessing | Header not found, macro errors |
|---|
Compilation | Syntax errors, type mismatches |
|---|
Assembly | Rare, but architecture-specific errors |
|---|
Linking | Undefined references, missing symbols |
|---|
Execution | Segmentation faults, logic bugs |
|---|
Explore
C++ Basics
Core Concepts
OOP in C++
Standard Template Library(STL)
Practice & Problems