-
Compiler phases (high-level)
- Preprocessor: copy-paste include file code
- Compiler: extended cpp -> assembly (main.s)
- Assembler: assembly -> machine code (main.o)
- Linker: links them together... but wtf does that mean?
-
Linker combines multiple object files (.o) into a single executable by
- Merges separate assembly code blocks (.text sections) into one file
- Fills in the actual memory addresses for function calls (like main() calling a function in math.o) so the CPU knows where to jump
-
Edgecases: Order of obj files sent to linker matters when using
- Static libraries
- Two libraries have fns with same name
- There exists circular deps between libraries
- But that's outside the scope of this video :)
# -o = name of output file
# -g = debug with lldb (useful later)
# -c = compile only and skip linker
clang++ -std=c++17 -g -c math.cpp -o math.o
clang++ -std=c++17 -g -c main.cpp -o main.o
# -d = disassemble
# -S = show source code alongside assembly
objdump -d -S math.o
objdump -d -S main.o
# linking main.o and math.o into main executable
clang++ main.o math.o -o main
# does the order matter?
clang++ math.o main.o -o main-
What Exactly Does the C++ Linker Do?
-
How the C++ Linker Builds Your Executable (Step-by-Step)
-
You Probably Didn't Know This About the C++ Linker
-
How the C++ Linker Works: Symbol Resolution & Relocation
-
How does the C++ compiler's linker work?
- cppreference.com
- learncpp.com
- ChatGPT and Gemini