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/config.toml file. You should also know the build phases from the earlier chapter: rustc, LLVM, then the linker.
Why the linker matters
The linker is the last step of a build. It combines your object files and libraries into one executable. It is mostly single-threaded and cannot start until every crate has been compiled.
This means link time is paid on every rebuild, even after a one-line change. With incremental compilation, rustc often finishes quickly and the linker becomes most of the wait. Large binaries, large test binaries, and debug builds with a lot of debug info make it worse.
Confirm the linker is a bottleneck
Do not switch linkers on a guess. Run a build after a trivial change and look at where the time goes. cargo build --timings shows per-crate time. If your final binary crate spends a long time after its compile work finishes, the link step is the likely cause.
A quick check is to touch a source file and rebuild:
touch src/main.rs
time cargo buildIf this takes several seconds on a small change, try a faster linker.
Choose a linker
Three linkers are common on Linux:
| Linker | Notes |
|---|---|
| GNU ld (`bfd`) | The traditional default on many systems. Slowest of the three. |
| lld | From the LLVM project. Fast, widely packaged, and works on several platforms. |
| mold | Written for speed on Linux. Often the fastest, and you must install it separately. |
Recent stable Rust toolchains on x86_64 Linux already use lld by default. If you are on an older toolchain, or on a different target, you still need to configure it yourself. Run rustc --version and check the release notes for your target.
Configure lld
Put the setting in .cargo/config.toml so it applies to the project. This asks clang to drive the link and pass -fuse-ld=lld to it.
# .cargo/config.toml
[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=lld"]To use mold, install it and change the flag to -fuse-ld=mold. Keep the rest the same.
# .cargo/config.toml
[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=mold"]Then rebuild and compare against your earlier timing.
touch src/main.rs
time cargo buildCommon errors
linker 'clang' not found: install clang. Thelinkersetting names a program that must be on yourPATH.unknown argument: '-fuse-ld=mold'orcannot find 'ld': your clang or gcc is too old to know about mold, or mold is not installed. Install a current version.- The change has no effect: the
[target.<triple>]table must match your host target exactly. Runrustc -vVand read thehost:line. - Different results on teammates' machines: a committed
.cargo/config.tomlforces everyone to have the same tools. Put personal choices in~/.cargo/config.tomlinstead.
Limits
A faster linker only shortens the link step. If your time goes to compiling a large crate or heavy dependencies, a different fix from this tutorial will help more. Also, linker flags matter only for the target you set. Cross-compiling needs its own [target.*] table.
Exercise
After touch src/main.rs, a rebuild of your binary crate takes 9 seconds. cargo build --timings shows rustc finishing in 2 seconds. What does this suggest, and what change would you try first?
Show answer
About 7 seconds are spent after compilation, which points to the link step. Try switching to lld or mold with a [target.<triple>] table in .cargo/config.toml, then time the same touch and rebuild again to compare.
Exercise
You add rustflags to .cargo/config.toml, but the build ignores them. Name one likely cause.
Show answer
The RUSTFLAGS environment variable is set, and Cargo uses it instead of the config file's rustflags. Another possible cause is that the target table does not match the host triple shown by rustc -vV.