Skip to content

Latest commit

 

History

History
77 lines (52 loc) · 1.91 KB

README.md

File metadata and controls

77 lines (52 loc) · 1.91 KB

Rust - cross compiling with Cargo

Usage

This is an example of cross-compiling from x86 (x86_64) to ARM (armv7).

  1. Install the GCC cross-compilation toolchain with your default package manager (apt-get, dnf, pacman, brew, ...)
$ sudo apt-get install gcc-arm-linux-gnueabihf
  1. Install the target platform with rustup
$ rustup target add armv7-unknown-linux-gnueabihf
  1. Within your project, create cargo config.toml file
$ mkdir .cargo
$ touch config.toml
  1. Update config.toml with cross compilation details
[build]
target = "armv7-unknown-linux-gnueabihf"

[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"
  1. 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

Alternatives

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

Resources

License

This repository is distributed under the terms of the MIT license. See terms and conditions here.