Learn C++
#include "gettingstarted/compilation_model.hpp"

The Compilation Model: Preprocess, Compile, Link in C++

//concept

Building a C++ program is a three-stage pipeline hidden behind the single g++ command. First the preprocessor handles # lines: it pastes headers in, expands macros, and strips comments. Then the compiler translates each .cpp file independently into an object file of machine code. Finally the linker stitches object files and libraries together into one executable.

Knowing the stages turns cryptic errors into easy ones: undefined reference is a linker error (declared but never defined or not linked in), while syntax and type errors come from the compiler stage. Headers (.h) carry declarations; source files (.cpp) carry definitions β€” splitting them is what makes multi-file projects possible.

//how it works
1

Preprocessing

#include pastes header text in, #define expands macros, and conditional blocks like #ifdef select code β€” the output is still C++ source, just bigger.

2

Compilation

Each .cpp file compiles separately into a .o object file. Syntax errors, type mismatches, and unknown names all fail here.

3

Linking

The linker combines object files and libraries into the final executable, resolving every function call to an actual definition.

4

Headers vs sources

Headers declare what's available (function signatures, class layouts); .cpp files define what things do. Including a header without ever defining its functions causes a linker error, not a compiler error.

//example
// math.hpp: int square(int x); (declaration)
// math.cpp: int square(int x) { return x * x; } (definition)
// main.cpp: #include "math.hpp" ... square(6) ...
// Build: g++ main.cpp math.cpp -o app
Output
36
1main.cpp includes only the declaration, so it compiles fine knowing square's signature without seeing its body.
2math.cpp compiles separately into its own object file containing the real square implementation.
3The linker joins both objects; forgetting math.cpp on the command line would fail here with undefined reference to square.
//use cases
Diagnosing undefined reference errors as missing definitions or missing files at link time
Splitting a growing single-file program into headers plus .cpp files that compile independently
Speeding up rebuilds: only changed .cpp files recompile, then everything relinks
Understanding what -c (compile only) and -o (name the output) flags do in real build commands

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C++ yourself β€” building beats watching.