This is my latest blog post:

Cross compiling with rust for the Raspberry Pi 3B

Cross compiling is not easy. This is a short how-to, and it will only concentrate on the cross part.

Only tested on Debian 12 as the host, and Raspberry Pi OS 64bit (Debian 12) as the target. (But this shouldn't be to complicated to change some part to fit also to an another system.)

In this post I only consider cross compilation of a simple rust program. I'll create another post for more complex setup.

Preconditions

Cross tool chain

Rust needs a dedicated tool chain to cross compile, and least an external linker for the target system. It depends on the host system, which one we need. I'll will use the Debian package, this should match, because I use the same version as the Raspberry Pi OS use. If you have any issue or your version is not matching, then you can install the cross compiler from here.

sudo apt install gcc-aarch64-linux-gnu
rustup target add aarch64-unknown-linux-gnu

I set up the rust project, that I don't need to specify the target every time, when compiling. This project can only run on the target. So compiling for the host system make no sense.

[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"
runner = "./deploy.sh"

[build]
target = ["aarch64-unknown-linux-gnu"]

All the options are explained in the cargo documentation.

The deploy.sh is a small shell script, and will be executed when the cargo run command is executed.

#!/bin/bash
scp $1 <user>@<raspberry pi host name>:<directory>/ && ssh <user>@<raspberry pi host name> <directory>/<executable>
  1. Every < > need to be changed to the project or target specific value
  2. The file need to be executable
  3. For convenience the ssh key should be added to the device. In this way we don't need to log in every time. Here is the official guide how to set up.

And now we can run the usual cargo commands to build and run the project.

Read more...