TExcelTAD
Home › Cargo in Depth › Cross-Compiling with --target

Cross-Compiling with --target

Rust is a cross-compiler out of the box: rustc can generate code for any target LLVM supports. What you need besides the compiler is a standard library built for the target and a linker that can produce the target's executables. Cargo ties the three together with --target and a few lines of config. (Build Fundamentals: Cross-Compilation explains host, build and target triples.)

The three ingredients

Ingredient How you get it On this course's machine
target std rustup target add aarch64-unknown-linux-gnu installed
linker (and C toolchain for C code) a cross GCC/Clang, or zig cc, or a container with both gcc-aarch64-linux-gnu 13.3 + arm64 libc
config [target.<triple>] linker = "..." in .cargo/config.toml written in the session
bash · try it yourself
rustup target list --installed
rustc --print target-list | grep -c .
rustc --print target-list | grep -E '^(aarch64-unknown-linux-gnu|x86_64-pc-windows-msvc|wasm32-unknown-unknown|thumbv7em-none-eabihf)
  

aarch64-unknown-linux-gnu
x86_64-unknown-linux-gnu
331
aarch64-unknown-linux-gnu
thumbv7em-none-eabihf
wasm32-unknown-unknown
x86_64-pc-windows-msvc

rustc knows every target in that list; the rustup target list --installed ones have a std ready to link.

A first cross build, and the linker error

bash · try it yourself
cargo new hello-arm --vcs none -q
cd hello-arm
cat > src/main.rs <<'RS'
fn main() {
    println!("hello from {} on {}", std::env::consts::ARCH, std::env::consts::OS);
}
RS
cargo build --release --target aarch64-unknown-linux-gnu 2>&1 | grep -E '^ *(Compiling|error)|rust-lld: error' | head -4
   Compiling hello-arm v0.1.0 (/home/learner/hello-arm)
error: linking with `cc` failed: exit status: 1
  = note: rust-lld: error: --fix-cortex-a53-843419 is only supported on AArch64
error: could not compile `hello-arm` (bin "hello-arm") due to 1 previous error

The Rust code compiled fine; linking failed. Cargo used the host's default linker setup, and a linker for x86-64 can't produce an ARM64 executable. Tell Cargo which linker to use for the target:

bash · try it yourself
cargo new hello-arm --vcs none -q
cd hello-arm
cat > src/main.rs <<'RS'
fn main() {
    println!("hello from {} on {}", std::env::consts::ARCH, std::env::consts::OS);
}
RS
mkdir -p .cargo
cat > .cargo/config.toml <<'T'
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"
T
cargo build --release --target aarch64-unknown-linux-gnu 2>&1
ls target
file target/aarch64-unknown-linux-gnu/release/hello-arm | cut -d, -f1-2
echo "--- native build for comparison"
cargo run -q --release
./target/aarch64-unknown-linux-gnu/release/hello-arm 2>&1 | sed 's/^.*: //'
   Compiling hello-arm v0.1.0 (/home/learner/hello-arm)
    Finished `release` profile [optimized] target(s) in 0.19s
CACHEDIR.TAG
aarch64-unknown-linux-gnu
release
target/aarch64-unknown-linux-gnu/release/hello-arm: ELF 64-bit LSB pie executable, ARM aarch64
--- native build for comparison
hello from x86_64 on linux
Exec format error
  • Output for a --target build goes to target/<triple>/<profile>/, separate from native builds.
  • file confirms an ARM aarch64 ELF executable. Running it on the x86-64 machine fails with Exec format error. To test it, copy it to an ARM machine, or set a runner (e.g. runner = "qemu-aarch64 -L /usr/aarch64-linux-gnu") so cargo run/cargo test --target ... execute through QEMU user-mode emulation.
  • The same setting as an environment variable: CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc, convenient in CI.

C code in a cross build

Build scripts run on the host but build for the target. The cc crate handles that automatically by picking the cross C compiler from the target triple:

bash · try it yourself
cargo new crc --vcs none -q
cd crc
mkdir -p csrc .cargo
echo 'unsigned triple(unsigned x) { return 3 * x; }' > csrc/triple.c
cat > build.rs <<'RS'
fn main() {
    println!("cargo::rerun-if-changed=csrc/triple.c");
    cc::Build::new().file("csrc/triple.c").compile("triple");
}
RS
cargo add --build cc -q
echo 'unsafe extern "C" { fn triple(x: u32) -> u32; }
fn main() { println!("{}", unsafe { triple(14) }); }' > src/main.rs
printf '[target.aarch64-unknown-linux-gnu]\nlinker = "aarch64-linux-gnu-gcc"\n' > .cargo/config.toml
cargo build --release --target aarch64-unknown-linux-gnu 2>&1 | grep -v Unpacking
echo "--- the C object (built by cc for the target)"
file target/aarch64-unknown-linux-gnu/release/build/crc-*/out/*triple.o | sed 's/.*: //' | cut -d, -f1-2
echo "--- the build script (built for the host, under target/release)"
file target/release/build/crc-*/build-script-build | sed 's/.*: //' | cut -d, -f1-2
   Compiling find-msvc-tools v0.1.15
   Compiling shlex v2.0.1
   Compiling cc v1.10.0
   Compiling crc v0.1.0 (/home/learner/crc)
    Finished `release` profile [optimized] target(s) in 1.51s
--- the C object (built by cc for the target)
ELF 64-bit LSB relocatable, ARM aarch64
--- the build script (built for the host, under target/release)
ELF 64-bit LSB pie executable, x86-64

Nobody told cc which C compiler to use: it derived aarch64-linux-gnu-gcc from the target triple, so the C object is ARM64 code. The build script itself is an x86-64 program that ran on the host, and it lives under target/release/, not under target/aarch64-unknown-linux-gnu/. Note that the build script and cc were compiled for the host: in a cross build, Cargo compiles build dependencies and proc-macros once for the host and normal dependencies for the target.

Beyond a cross GCC

Approach Good for
distro cross toolchain + linker = ... (this lesson) Linux → Linux, simple C dependencies
cross (cross build --target ...) runs Cargo inside a Docker image with the right toolchain and QEMU; zero local setup
cargo-zigbuild uses zig cc as a linker for any target; can target an older glibc (--target x86_64-unknown-linux-gnu.2.28)
*-unknown-linux-musl targets fully static Linux binaries, no glibc version issues
wasm32-*, thumbv*-none-* WebAssembly and bare-metal embedded (no_std)
macOS / Windows MSVC targets from Linux possible (osxcross, cargo-xwin), but SDK licences and toolchains make CI on the real OS the usual choice

Test yourself

cargo build --target aarch64-unknown-linux-gnu compiles but fails at the end. What is usually missing?

  1. The Rust source for ARM
  2. A linker for the target, configured with [target.aarch64-unknown-linux-gnu] linker = ...
  3. A Cargo.lock for ARM
  4. The --release flag

Where does the ARM release binary end up?

  1. target/release/
  2. target/aarch64-unknown-linux-gnu/release/
  3. target/arm/
  4. target/cross/release/

In a cross build, for which machine is build.rs compiled?

  1. The target
  2. The host, because it runs during the build
  3. Both
  4. Neither: it's interpreted