1. Intro
Unlike interpreted languages, C++ source code must be translated into machine code before it can run. This translation happens through several distinct stages, and understanding them helps you diagnose errors like missing header, undefined reference, or linker error.
Cricket analogy: Just as raw net-practice footage must go through selection, fitness tests and final XI announcement before a player takes the field, C++ source code passes through several translation stages before it becomes a runnable program, and skipping steps causes errors like a 'linker error'.
2. Syntax
# Typical g++ compilation pipeline for a single file
g++ -E hello.cpp -o hello.i # 1. Preprocessing
g++ -S hello.i -o hello.s # 2. Compilation to assembly
g++ -c hello.s -o hello.o # 3. Assembly to object code
g++ hello.o -o hello # 4. Linking to executable
# In practice, one command does all four stages:
g++ hello.cpp -o hello
./hello3. Explanation
The compilation process has four broad stages. First, the preprocessor handles directives beginning with #, such as #include and #define, expanding headers and macros into a single translation unit. Second, the compiler translates this preprocessed source code into low-level assembly code specific to the target CPU architecture, also performing syntax and type checking — this is where most compile-time errors are caught. Third, the assembler converts the assembly code into object code (machine code in binary form, typically a .o or .obj file), which is not yet a runnable program. Finally, the linker combines one or more object files with the required library code (such as the C++ standard library) to resolve function calls like cout and produces a single executable file. If a function is declared but never defined anywhere the linker can find, you get a linker error (undefined reference), which is different from a compiler error.
Cricket analogy: The preprocessor is like collecting every player's registration form (#include) and abbreviations (#define) into one squad list; the compiler checks each player's eligibility and skill (syntax/type checking) like BCCI selectors; the assembler drafts the actual playing XI as an unsigned team sheet (object code); and the linker is the match referee who confirms every player, including guest imports like an overseas Sunil Narine (library functions), is actually present before play starts — a missing player causes a 'linker error'.
4. Example
#include <iostream>
using namespace std;
int main() {
cout << "Compiling C++ step by step!" << endl;
return 0;
}5. Output
Compiling C++ step by step!6. Key Takeaways
- C++ compilation has four stages: preprocessing, compilation, assembly, and linking.
- The
preprocessorexpands#includeand#definedirectives before real compilation begins. - The
compilerchecks syntax/types and produces assembly code; theassemblerturns it into object code. - The
linkercombines object files and libraries into the final executable, and reportslinker errorsfor unresolved references.
Practice what you learned
1. What is the first stage of the C++ compilation process?
2. What does the compiler stage primarily check?
3. What is produced by the assembler stage?
4. What is the role of the linker?
5. An `undefined reference` error typically occurs at which stage?
Was this page helpful?
You May Also Like
Your First C++ Program (Hello World)
Write, compile, and run your first C++ program — the classic Hello World example — with a line-by-line explanation of every part.
Structure of a C++ Program
Understand the essential building blocks of a C++ program — headers, namespaces, the main function, and statements — with an annotated example.
History and Evolution of C++
Learn how C++ evolved from `C with Classes` into a modern, standardized systems programming language used across the software industry today.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics