Rust Cross-Compilation Linker Missing in CI
You added the cross-compilation target and rustc compiled the code, but the link step needs a cross-linker for the foreign architecture that isn’t installed. The host cc can’t link a binary for a different CPU target.
What this error means
A --target <foreign> build gets through compilation, then fails at link with linker <arch>-gcc not found (or a default cc producing wrong-architecture errors). The target’s std is installed, but the cross C toolchain/linker is missing.
error: linker `aarch64-linux-gnu-gcc` not found
|
= note: No such file or directory (os error 2)
error: could not compile `app` (bin "app") due to 1 previous errorCommon causes
No cross-linker for the target architecture
Cross-compiling to aarch64/arm/musl needs a matching cross GCC (or lld) and often the target’s C runtime. Adding the rustc target alone doesn’t install the linker.
Cargo not told which linker to use
Even with the cross-linker installed, Cargo defaults to cc. Without a per-target linker = ... in .cargo/config.toml, it invokes the host linker for the foreign target.
How to fix it
Install the cross toolchain and point Cargo at it
Add the cross GCC, then set the linker for that target.
apt-get update && apt-get install -y gcc-aarch64-linux-gnu
# .cargo/config.toml
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"Or use a cross-build tool
Tools like cross (Docker-based) or cargo-zigbuild bundle the right toolchain so you don’t configure linkers by hand.
cargo install cross --git https://github.com/cross-rs/cross
cross build --target aarch64-unknown-linux-gnu --releaseHow to prevent it
- Install the matching cross GCC/linker for every target you build.
- Set a per-target
linkerin.cargo/config.toml. - Use
crossorcargo-zigbuildfor hands-off cross toolchains in CI.