Loading lessons...
The Compiler, the Linker, and Libraries
The Compiler, the Linker, and Libraries
Going from your shiny source file to a running program is not magic, it's a pipeline. Meet the compiler, the assembler, and the linker.
The build pipeline
- Preprocessor - handles directives that start with
#, like#include, by pulling in files and definitions. - Compiling - the compiler translates your source code into assembly, a human-readable machine language for your CPU.
- Assembling - an assembler turns the assembly into machine code, saved as an object file (
.oor.obj). - Linking - the linker merges all your object files (plus any libraries) into a single executable.
Object files (.o / .obj)
Each .cpp file compiles into its own object file containing machine code. Alone, an object file can't run yet - it's just a piece of the puzzle.
The linker's job
The linker is the glue:
- combines object files into one executable
- resolves references (who is defined where?)
- merges in library code as needed
- produces the final program you can run
Static libraries
A library is reusable code someone already wrote, it's the classic "don't reinvent the wheel". A static library gets copied fully into your executable at link time, so your program carries a copy of it.
Development tools
An IDE (like Visual Studio or VS Code) wraps all this up so "Build" just works. Underneath, the same preprocess, compile, assemble, link chain is running.
TL;DR
- The pipeline is: preprocess, compile, assemble, link.
- Each source file becomes an object file (
.oor.obj). - The linker combines object files and libraries into a runnable program.
- Static library code is copied straight into the executable.