Sub topic 8
Switching to a Faster Linker
This chapter assumes you can build Rust projects with Cargo, edit Cargo.toml, and read a cargo build --timings report. You should also know the rough split between rustc, LLVM, and the linker from the first chapter.
What the linker does and why it matters
After rustc and LLVM produce object files, the linker combines them with your dependencies and system libraries into one executable or shared library. It resolves symbols, lays out sections, applies relocations, and writes the output file.
The link step runs on every build that changes the final binary, and incremental compilation does not shorten it. Incremental compilation helps rustc and LLVM reuse earlier work. The linker still processes all the inputs again. After a one-line edit, compiling may take under a second while linking takes several seconds. That is common in these cases:
- Large binaries with many dependencies.
- Debug builds, because debug info makes the object files much bigger.
- Test builds, where each integration test file becomes its own binary and is linked separately.
If the link step is small in your project, switching linkers will gain you little. Measure first.
Confirm that linking is the bottleneck
Make a trivial change, such as editing a string literal, and time the rebuild:
touch src/main.rs
cargo build --timingsOpen target/cargo-timings/cargo-timing.html and look at the bar for your final crate. The bar includes rustc's work and the link. To see the link alone, ask rustc to print its own timings with a nightly toolchain:
RUSTFLAGS="-Z time-passes" cargo +nightly build 2>&1 | grep -i linkOn stable, you can compare against a known-fast linker. If the rebuild gets noticeably faster after switching, linking was a real part of the cost.
The linker options
| Linker | Platform | Notes |
|---|---|---|
| GNU ld (`bfd`) | Linux | Historical default on many distributions. Single-threaded for most work. |
| GNU gold | Linux | Faster than bfd, but now in maintenance mode. |
| lld | Linux, macOS, Windows | LLVM's linker. Multi-threaded and fast. Ships with Rust as `rust-lld`. |
| mold | Linux (and others) | Very fast, designed for parallelism. Must be installed separately. |
| ld64 / ld-prime | macOS | Apple's linker. Recent Xcode versions are already much faster than older ones. |
| MSVC `link.exe` | Windows | Default for the MSVC target. `lld-link` is an alternative. |
Which one is "best" depends on your operating system, your toolchain version, and your project. mold is usually the fastest on Linux. lld is the easiest to adopt because it needs no separate install on recent toolchains.
Recent defaults
Newer Rust releases have changed the default on one platform. On x86_64-unknown-linux-gnu, recent stable toolchains use rust-lld by default instead of the system linker. If your toolchain is recent, you may already have the benefit. Check which linker rustc invokes before changing anything:
cargo clean
RUSTFLAGS="--print link-args" cargo build 2>&1 | grep -E "lld|mold|gold|ld" | headLook at the command rustc runs. If you see -fuse-ld=lld or a path containing rust-lld, you are already using lld. If you target another platform or an older toolchain, configure the linker yourself as shown below.
Configure lld on Linux
The usual method is to tell rustc to link through clang and pass a flag that selects the linker. Put this in .cargo/config.toml at the project root, or in ~/.cargo/config.toml to apply to every project:
# .cargo/config.toml
[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=lld"]This requires clang and lld to be installed. On Debian or Ubuntu, sudo apt install clang lld provides both. The linker = "clang" line makes rustc call clang as the linker driver. The rustflags line passes -fuse-ld=lld to clang, so clang calls lld instead of the system default.
You can also use gcc as the driver. Recent GCC versions accept -fuse-ld=lld and -fuse-ld=mold, so installing clang is not strictly necessary.
Configure mold
To use mold, install it and change one word:
# .cargo/config.toml
[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=mold"]An alternative is to run Cargo through mold's wrapper, which replaces the linker for the child process:
mold -run cargo buildThe wrapper approach is useful for trying mold once without editing any files. If it helps, move the setting into the config file.
For ARM Linux, change the target triple to aarch64-unknown-linux-gnu. Each target table applies only to that triple, so a config for x86-64 does nothing on an ARM machine.
Where to put the configuration
Cargo reads config.toml files from several places and merges them. This matters because a linker setting does not belong in every place.
- Project
.cargo/config.toml, committed to version control. Everyone on the team and your CI get the setting. This fails for people who do not have the linker installed. ~/.cargo/config.toml. Applies only to your machine. Use this for personal preferences such as mold.- Environment variables.
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGSoverrides config values for one invocation. This suits CI jobs.
A common arrangement is to keep the committed config empty of linker settings and put the fast linker in your home directory. That way a teammate on macOS, or a CI image without mold, is never broken by your preference.
Windows and macOS
On Windows with the MSVC toolchain, link.exe is the default. You can switch to lld-link:
# .cargo/config.toml
[target.x86_64-pc-windows-msvc]
linker = "rust-lld.exe"Results vary. Windows builds often spend much of their link time scanning debug info, so reducing debug info (see the profile chapter) may help more than changing the linker.
On macOS, Apple's linker has improved considerably in recent Xcode releases. Update Xcode or the command line tools first and measure again. Some projects also see gains from setting split-debuginfo = "unpacked" in the dev profile, which keeps debug info out of the linker's critical path:
# Cargo.toml
[profile.dev]
split-debuginfo = "unpacked"macOS already defaults to this behavior for dev builds on current toolchains, so you only need to set it if you have overridden it.
Reduce what the linker has to do
A faster linker is one lever. The other is giving the linker less work.
- Reduce debug info. Setting
debug = "line-tables-only"in[profile.dev]shrinks object files. Smaller inputs link faster. - Fewer test binaries. Each file in
tests/links separately. Combining them into one binary reduces the number of links. A later chapter covers this. - Fewer dependencies. Every crate adds object code to link.
- Dynamic linking of heavy shared code. Some projects, such as game engines, build a large dependency as a dynamic library in development. This cuts link time sharply but adds complexity and is rarely worth it outside those cases.
Common errors
linker 'clang' not found. Clang is not installed or not onPATH. Install it, or usegccas the driver.cannot find ldorunknown argument: '-fuse-ld=...'. The driver you chose does not support that linker name, or the linker is not installed. Check the versions withclang --versionandld.lld --version.- Builds work locally but fail in CI. You committed a linker setting that the CI image does not have. Either install the linker in the image or move the setting to your personal config.
- Cross-compilation breaks. A
[target.x86_64-unknown-linux-gnu]table applies when that is the target. When you cross-compile to another triple, you need a separate table with a linker that can produce that target. - Runtime differences. In rare cases, a program linked with a different linker behaves differently, for example with unusual symbol visibility or static constructors. If you see strange behavior only in one configuration, rebuild with the default linker to rule the linker out. Release builds you ship can keep the conservative linker while dev builds use the fast one, by using separate config or environment settings.
A worked comparison
Here is a method for deciding with data rather than with folklore. Run each variant on a clean checkout, then repeat the one-line-edit rebuild three times and take the median.
cargo build
for i in 1 2 3; do
touch src/main.rs
/usr/bin/time -f "%e s" cargo build
doneRepeat after changing the config to lld, then to mold. Keep the same toolchain and the same machine state. Record the numbers in a small table so the team can see the choice and the reason for it. Check again after toolchain upgrades, because defaults and performance change.
Exercise
A teammate on macOS cannot build after you commit linker = "clang" with -fuse-ld=mold in the project's .cargo/config.toml. Where should the setting live instead, and how can you confirm that the linker is worth switching to before you change anything?
Show answer
Move the setting to your personal ~/.cargo/config.toml, so only your machine uses mold. Before switching, touch a source file and time an incremental rebuild with cargo build --timings, then rerun it with the new linker. If the rebuild gets noticeably faster, linking was a real cost. If it does not change, keep the default.
Exercise
After adding rustflags = ["-C", "link-arg=-fuse-ld=lld"] to a target table, you notice that an RUSTFLAGS="-C target-cpu=native" environment variable you set in your shell now seems to disable the lld setting. Why?
Show answer
When the RUSTFLAGS environment variable is set, Cargo uses it and ignores rustflags from config files. The lld flag is therefore never passed. Put every flag in one place, or include -C link-arg=-fuse-ld=lld in the environment variable as well.