skillfed

linkers-lto

Master linker selection, essential flags, and link-order resolution for GNU ld, gold, and lld. Learn to enable LTO with GCC or Clang, strip dead code with --gc-sections, and debug common linker errors like undefined symbols and circular dependencies.

Linkers and LTO helps you resolve undefined reference errors, fix link-order issues, and safely enable link-time optimization in your projects.

AI-generated summary based on this skill's SKILL.md

148 19 MIT updated by mohitmishra786

Install

mohitmishra786/low-level-dev-skills/linkers-lto · repository language: JavaScript

git clone https://github.com/mohitmishra786/low-level-dev-skills
cp -r low-level-dev-skills/skills/binaries/linkers-lto ~/.claude/skills/linkers-lto
npx skillfed install mohitmishra786/low-level-dev-skills/linkers-lto

Frequently asked questions

AI-generated answers based on this skill's SKILL.md and metadata

What does linkers-lto cover?

linkers-lto teaches you how to master linker selection, essential flags, and link-order resolution for GNU ld, gold, and lld. You'll learn to enable LTO with GCC or Clang, strip dead code with --gc-sections, and debug common linker errors like undefined symbols and circular dependencies.

How do I fix an undefined reference linker error?

linkers-lto addresses undefined reference errors by teaching link-order resolution and proper library linking. Common causes include incorrect library order (libraries must come after object files that use them), missing libraries, or circular dependencies. Use -Wl,--start-group and -Wl,--end-group to handle circular deps, and check your linker map file to verify symbol resolution.

How do I enable LTO in my C++ project?

linkers-lto shows how to enable full or thin LTO for production builds. Add -flto to both compilation and linking flags with GCC or Clang. For thin LTO (faster), use -flto=thin. When using archives, use gcc-ar and gcc-ranlib instead of standard ar and ranlib to preserve LTO metadata. Enable LTO consistently across all object files and libraries.

Which linker is fastest: gcc ld, gold, or lld?

linkers-lto covers choosing and configuring the right linker for your needs. GNU ld is the default but slower; gold is faster for large projects; lld is the fastest modern option. Select gold with -fuse-ld=gold or lld with -fuse-ld=lld. Performance depends on project size and complexity, so benchmark your specific build to decide.

How can I reduce binary size with gc-sections?

linkers-lto teaches dead-code stripping via section garbage collection. Compile with -ffunction-sections -fdata-sections to place each function and data item in its own section, then link with -Wl,--gc-sections to remove unused sections. This significantly reduces binary size in production builds by eliminating unreachable code and unused data.

What are the trade-offs of link time optimization?

linkers-lto explains LTO trade-offs for informed production decisions. LTO enables whole-program optimization and better dead-code elimination, improving performance and reducing size. However, it increases compilation time substantially and requires more memory during linking. Thin LTO offers a middle ground with faster builds than full LTO but less optimization.

SKILL.md

rendered from the published skill — quoted content, verbatim

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 reference at 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
Linker Invocation Strengths
GNU ld (BFD) default on Linux Universal, stable
gold -fuse-ld=gold Faster than ld for C++; supports LTO plugins
lld (LLVM) -fuse-ld=lld Fastest, parallel, required

(truncated - see the full file via the links below)

Read as markdown · JSON record · Browse the source repository

File tree — 2 files
skills/binaries/linkers-lto/SKILL.md
skills/binaries/linkers-lto/references/flags.md

Related skills

Tags

inter-module-optimization binary-stripping symbol-resolution link-stage-errors linker-selection code-elimination relocation-handling archive-management