Linkers and LTO
Purpose
Guide agents through linker selection, common linker flags, link-order issues, LTO setup, and symbol-visibility management.
Triggers
- "I'm getting
undefined referenceat link time" - "How do I enable LTO for a real project?"
- "Which linker should I use: ld, gold, or lld?"
- "How do I reduce binary size with
--gc-sections?" - "How do I write or understand a linker script?"
- "I have duplicate symbol or weak symbol issues"
Workflow
1. Linker selection
2. Essential linker flags
3. Link order matters (GNU ld)
GNU ld processes archives left-to-right. A library must come after the objects that need it.
4. LTO with GCC
Parallel LTO:
5. LTO with Clang / lld
ThinLTO caches work: subsequent builds that reuse unchanged modules are faster.
Cache location: specify with -Wl,--thinlto-cache-dir=/tmp/thinlto-cache.
6. Dead-code stripping
On macOS, the linker strips dead code by default (-dead_strip).
7. Symbol visibility
Controlling visibility reduces DSO size and enables better LTO:
Or use a version script:
8. Common linker errors
9. Linker map file
Useful for debugging binary size and symbol placement.
For a comprehensive linker and LTO flags reference, see references/flags.md [blocked].
Related skills
- Use
skills/binaries/elf-inspectionfor examining the resulting binary - Use
skills/compilers/gccorskills/compilers/clangfor compile-phase LTO flags - Use
skills/binaries/binutilsforar,strip,objcopy


