Cross Compiling Rust for the Raspberry Pi on Linux

Laurent Zuijdwijk
HackerNoon.com
3 min readJan 15, 2017

--

Lets build a simple “Hello Pi” program in Rust and compile it for the Raspberry Pi running a Linux version. I am using a Raspberry Pi 3 and Raspbian. Why do we want to compile this on a PC? Assuming you do development of your application on a PC, it would be much faster to compile and test it on a PC as well and ship only the binary to the RPi.

We will need rustup in order to use different versions of Rust and to select compile targets. It is a toolchain installer.

If you have a previously installed Rust, then rustup will ask you to remove it, so that rustup can manage the different Rust versions. Go ahead and install the default Rust version for your environment.

To compile for the Raspberry Pi we will need to install the right toolchain for the ARM platform. Model 2 and 3 support ARMv7, while model 1 supports ARMv6. The RPi has a floating point processor, so we want the gnueabihf (hf is hard float I believe), as opposed to the gnueabi. This article will focus on models 2&3.

Add the toolchain for Rust:

and the right C compiler:

We need to add our build target to ~/.cargo/config by adding the following lines, so that rust knows which linker to use.

Creating our project

Installing Rust will have installed cargo, the Rust package manager. We can use it to create a new project.

We can still run our project locally on AMD64, by running cargo run. To build our project for the RPi we use.

Let’s see if we have successfully created an arm binary

Success! When we transfer hello-rpi to our Raspberry Pi, we will be able to run it and get a fantastic “hello world!” on the command line! Now that we can compile, we can start developing something more interesting.

Part 2 coming soon…

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

--

--

Laurent Zuijdwijk
HackerNoon.com

Staff engineer at Doxy.me | Follow me for software engineering, performance and architecture content. Find me at https://tinyurl.com/32em29kr