Skip to main content

Rust Crosscompile

·1 min

How to compile a rust project for RaspberryPi3 and later on MacOS #

rustup target add armv7-unknown-linux-musleabihf

Using musleabihf instead of gnueabihf to allow for statically linked binaries.

brew reinstall arm-linux-gnueabihf-binutils

Get binutils, primarily arm-linux-gnueabihf-ld.

brew tap messense/macos-cross-toolchains
brew install armv7-unknown-linux-musleabihf

Some crates might still have C dependencies, thankfully somebody offers a vast array of cross compile toolchains.

ln -s /opt/homebrew/bin/armv7-unknown-linux-musleabihf-gcc /opt/homebrew/bin/arm-linux-musleabihf-gcc

The toolchain doesn’t follow the naming convention the rust build system looks for, so we create a symbolic link for gcc to be found.

mkdir .cargo

Create a cargo configuration directory in your project root.

# Configure linker for rpi builds
[target.armv7-unknown-linux-musleabihf]
linker = "armv7-unknown-linux-musleabihf-ld

We tell cargo which linker to use for our armv7-unknown-linux-musleabihf target.

Finally we can call cargo as follows.

cargo build --release --target=armv7-unknown-linux-musleabihf

You can find the resulting binary here. (Replace ${BINARY_NAME} with the name of your project.)

./target/armv7-unknown-linux-musleabihf/release/${BINARY_NAME}