Created: September, 1, 2023 Last Modified: September, 1, 2023

Cross Compiling Rust Code Using Cross For the Raspberry Pi Zero

Cross uses Docker images in order to enable easy cross compilation. In this post I am going to go over cross compiling Rust code for the Raspberry Pi Zero using the cross tool. Note that this will work for many cross compiling targets, it is just a matter of selecting the correct target. This tutorial shows the correct options for the Raspberry Pi Zero W

Installation


First make sure you have Cargo installed and update and Docker. Next we will use cargo to install cross run cargo install cross.

Creating The Project


Lets create a simple binary project that we will cross compile. With rust that can be done by running cargo init rust_cross rust_cross is the project/binary name

Building The Project


Next simplifying run cross build --target arm-unknown-linux-gnueabihf and watch the magic happen. Cross will automatically instll the correct rust toolchain and download the Docker image for the target if supported. A list of supported targets can be found (here)[https://github.com/cross-rs/cross#supported-targets].

Copying The Binary To The Pi and Runnning


Lastly we can use SCP to copy the binary to the pi and test if everything was built correctly. Run scp target/arm-unknown-linux-gnueabihf/debug/rust_cross root@pi-domain-or-ip:/home/user/binaries/rust_cross replacing the hostname with the Pi's ip and the /home/user/binaries with whatever directory you want to copy the binary to. Now on the pi we can cd to the directory and run the program. If everything built correctly you should see

root@pg3-pizerow:~/binaries# ./rust_cross 
Hello, world!