This is an example of cross-compiling from x86 (x86_64
) to ARM (armv7
).
- Install the GCC cross-compilation toolchain with your default package manager (apt-get, dnf, pacman, brew, ...)
$ sudo apt-get install gcc-arm-linux-gnueabihf
- Install the target platform with rustup
$ rustup target add armv7-unknown-linux-gnueabihf
- Within your project, create cargo
config.toml
file
$ mkdir .cargo
$ touch config.toml
- Update
config.toml
with cross compilation details
[build]
target = "armv7-unknown-linux-gnueabihf"
[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"
- Build and test your excecutable on the target system
# build normally on host
$ cargo build
# copy to target
$ scp target/armv7-unknown-linux-gnueabihf/debug/cross-compile user@arm:~
# run on target
$ ssh user@arm:~ ./cross-compile
Rust cross compiled from x86_64 -> aarch64
You can choose to manually select your compiler of choice each build of the project.
# Test locally on x86
cargo build --target=x86_64-unknown-linux-gnu
# Test remotely on ARM
cargo build --target=armv7-unknown-linux-gnueabihf
If you find yourself doing a lot of local testing, you can comment out the targets in the config.toml
file. Then you can test locally as you normally would:
# Test locally on x86 - after commenting out config.toml
cargo build
- Cross-compilation: rustup book
- Platform Support: rustc book
- More example code: japaric/rust-cross
This repository is distributed under the terms of the MIT license. See terms and conditions here.