Skip to content

Instantly share code, notes, and snippets.

@feymartynov
Created May 9, 2020 00:40
Show Gist options
  • Save feymartynov/81de01d0b95891c64dabc6b0cf1d8a42 to your computer and use it in GitHub Desktop.
Save feymartynov/81de01d0b95891c64dabc6b0cf1d8a42 to your computer and use it in GitHub Desktop.
An example of cross compiling Hello World program in Rust for Raspberry Pi Zero

Rust cross compilation for Raspberry Pi Zero

Create a Hello World project:

cargo new hello_rpi
cd hello_rpi

Create .cargo/config.toml in the project directory with the following contents:

[build]
target = "arm-unknown-linux-musleabihf"
rustflags = ["-C", "linker=arm-linux-gnueabihf-gcc"]

Here's a Dockerfile for building:

FROM rust:buster

RUN rustup target add arm-unknown-linux-musleabihf
RUN apt update && apt-get install -y gcc-arm-linux-gnueabihf

WORKDIR /build
COPY . /build
RUN cargo build --release

And here's deploy.sh script for running the build, extracting the articfact and deploying it to the Raspberry Pi.

It assumes that the Pi is accessible via SSH with pi@raspberrypi.local.

#/bin/bash -e

docker build -t hello_rpi:latest .
docker run -d -it --rm --name hello_rpi_artifact hello_rpi:latest
docker cp hello_rpi_artifact:/build/target/arm-unknown-linux-musleabihf/release/hello_rpi .
docker stop hello_rpi_artifact
scp ./hello_rpi pi@raspberrypi.local:~/hello_rpi
rm ./hello_rpi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment