The Linking Process
Linking combines object files and libraries into a final executable, resolving symbols and fixing addresses.
Linking Process Visualizer
Follow object files through symbol resolution and relocation, then inspect where each external reference is supplied.
Linking Pipeline
- 1Current
Input Processing
Read object files and libraries
Parsing ELF headers and section tables
- 2Upcoming
Symbol Collection
Build global symbol table
- 3Upcoming
Symbol Resolution
Match undefined symbols with definitions
- 4Upcoming
Section Merging
Combine similar sections
- 5Upcoming
Relocation
Fix addresses and offsets
- 6Upcoming
Output Generation
Write executable file
Input Processing: Parsing ELF headers and section tables.
Input Object Files
Linked Libraries
libm.so
dynamic · 156 KB
Provides: sqrt, log, sin, cos, tan, exp
libc.so
dynamic · 2.1 MB
Provides: printf, sprintf, malloc, free, exit
ld -o program main.o math.o utils.o -lm -lc --dynamic-linker /lib64/ld-linux-x86-64.so.2What the Linker Does
-
- Match undefined symbols with definitions
- Handle name mangling
- Resolve weak/strong symbols
-
Section Merging
- Combine .text sections (code)
- Merge .data sections (initialized data)
- Unite .bss sections (uninitialized data)
-
Relocation
- Adjust addresses to final locations
- Fix function calls and data references
- Update jump targets
-
Library Handling
- Include needed functions from libraries
- Static: Copy code into executable
- Dynamic: Create references for runtime
Types of Linking
Static Linking
The linker copies the object code it needs from a static archive into the executable, resolving symbols at link time:
# Create static library
ar rcs libstatic.a file1.o file2.o
# Link statically
g++ main.o libstatic.a -o program
Dynamic Linking
The executable only records the shared libraries it needs; the dynamic linker resolves them at load time:
# Create shared library
g++ -shared -fPIC -o libshared.so file1.o file2.o
# Link dynamically
g++ main.o -L. -lshared -o program
Common Link Commands
# Basic linking
g++ main.o utils.o -o program
# With libraries
g++ main.o -lm -lpthread -o program
# Specify library path
g++ main.o -L/usr/local/lib -lmylib
# View symbols
nm program
# Check dependencies
ldd program
Link Order Matters
Check the command-line order
Libraries should come after the objects that use them:
# Wrong (may fail)
g++ -lmylib main.o
# Correct
g++ main.o -lmylib
# Circular dependencies
g++ main.o -lA -lB -lA
Linking Topics
Deep dive into specific aspects:
- Symbol Resolution - How symbols are matched
- Dynamic Linking - Runtime library loading
- Static vs Dynamic - Trade-offs and use cases
What's Next?
After linking creates the executable:
- Program Loading - How the OS loads your program
- Memory Layout - Runtime memory organization
Related Topics
Related concepts
Follow a C++ function from tokens to a simplified AST, then inspect exact source ranges, declaration references and types in a keyboard-accessible explorer.
Understand the complete C++ compilation pipeline from source code to object files. Learn preprocessing, parsing, code generation, and optimization stages.
C++ compiler optimization lab notebook — compare optimization levels, inspect compiler rewrites, diagnose auto-vectorization, and build production verification commands.
C++ preprocessor visualized: macros, header guards, conditional compilation, and #include directives explained interactively.
How Linux loads C++ programs: captured ELF segments, runtime mappings, startup handoffs, lazy binding and practical loader debugging.
Complete guide to C++ symbol resolution — how linkers match references to definitions, name mangling, strong vs weak symbols, ODR, template instantiation, linking order, and debugging undefined reference errors.
