29

I recently acquired a Raspberry PI 2 and I want to run a Rust program on it.

Is there a guide/instructions how to cross compile Rust programs on Raspberry PI 2? I've heard about running Rust on RPi or Arduino, although not recently.

I want a Hello World equivalent Rust program running on Raspberry Pi 2. It doesn't have to be a literal Hello World program, just something that is of similar low complexity.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Daniel Fath
  • 16,453
  • 7
  • 47
  • 82
  • 4
    Some useful links: http://metaverse.fr/blog/compiling-rust-for-the-raspberry-pi/ and https://github.com/npryce/rusty-pi/blob/master/doc/compile-the-compiler.asciidoc – Filipe Gonçalves Apr 28 '15 at 11:24
  • Do you wish to *run* `rustc` on the RPi, or to you simply want to cross-compile a program that will execute on the RPi? – Shepmaster Apr 28 '15 at 13:31
  • @Shepmaster I want to cross-compile program to execute on RPi2. I want to run something simple on it. – Daniel Fath Apr 28 '15 at 13:32
  • @FilipeGonçalves How compatible is RPi with RPi2? That seems like a decent answer. – Daniel Fath Apr 28 '15 at 15:05
  • @DanielFath Honestly, I have no idea. I came up with those links after a quick google search. I will try and look into this later today if I have the chance. – Filipe Gonçalves Apr 28 '15 at 15:52

3 Answers3

29

We have rustup now.

$ rustup target add arm-unknown-linux-gnueabihf
$ sudo apt-get install gcc-arm-linux-gnueabihf
$ echo '[target.arm-unknown-linux-gnueabihf]' >> ~/.cargo/config
$ echo 'linker = "arm-linux-gnueabihf-gcc"' >> ~/.cargo/config
$ cd <project dir>
$ cargo build --target=arm-unknown-linux-gnueabihf
Chris Pick
  • 570
  • 4
  • 7
kazhik
  • 556
  • 1
  • 5
  • 7
  • This works perfectly fine. I just had to add the missing quote-escapes (`"linker = \"arm-linux-gnueabihf-gcc\""`). Would you mind to update your answer? – Kai Giebeler Oct 29 '17 at 12:47
  • This answer works fine on Raspberry Pi 2s and 3s, but not on 1s or Zeros. See [my answer](https://stackoverflow.com/a/48087233/291228) for a setup that builds binaries that work across all Pi versions. – Chris Pick Jan 04 '18 at 00:53
  • 1
    I had to use `sudo apt install gcc-arm-linux-gnueabihf libc6-dev-armhf-cross libc6-dev-armhf-cross binutils-doc gcc-7-locales cpp-doc gcc-7-multilib-arm-linux-gnueabihf gcc-7-doc libgcc1-dbg-armhf-cross` on Ubuntu 18.04. – cstroe Nov 14 '18 at 07:13
9

The Rust compiler is not distributed as a cross-compiler for the Raspberry Pi, so it needs to be compiled as a cross compiler with rpi dev tools.

  1. Get rpi dev tools - git clone https://github.com/raspberrypi/tools.git ~/pi-tools

  2. get rust compiler from mozilla git repo and add rpi tools to the path export PATH=~/pi-tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin:$PATH

  3. Look for rusty-pi dir on your home ./configure --target=arm-unknown-linux-gnueabihf --prefix=$HOME/rusty-pi && make && make install

  4. Considering helloworld.rs -> % ~/pi-rust/bin/rustc --target=arm-unknown-linux-gnueabihf -C linker=arm-linux-gnueabihf-g++ helloworld.rs

It will produce an executable.

druuu
  • 1,676
  • 6
  • 19
  • 36
9

@kazhik's answer will work for Raspberry Pi 2s and 3s (which are ARMv7/8 based), but not for Raspberry Pi 1s or Zeros (which are ARMv6 based).

The problem is that Debian/Ubuntu's armhf port (and thus their gcc-arm-linux-gnueabihf package/compiler/toolchain) targets >= ARMv7.

Fortunately, rustup's gcc-arm-linux-gnueabihf targets >= ARMv6 (with hardware floating-point, which all Raspberry Pis support), so all that's needed is the correct linker. The Raspberry Pi foundation provides one of those in their tools repository.

Putting it together, the following steps can be used to cross compile a Rust binary that works on all Raspberry Pis:

$ rustup target add arm-unknown-linux-gnueabihf
$ git clone --depth=1 https://github.com/raspberrypi/tools raspberrypi-tools
$ echo "[target.arm-unknown-linux-gnueabihf]" >> ~/.cargo/config
$ echo "linker = \"$(pwd)/raspberrypi-tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-gcc\"" >> ~/.cargo/config

To test the cross-compiler (assuming a Pi is running and reachable with the default raspberrypi hostname):

cpick@devhost:  $ cargo new --bin rpi-test
cpick@devhost:  $ cd rpi-test
cpick@devhost:  $ cargo build --target=arm-unknown-linux-gnueabihf
cpick@devhost:  $ scp target/arm-unknown-linux-gnueabihf/debug/rpi-test pi@raspberrypi:
cpick@devhost:  $ ssh pi@raspberrypi
pi@raspberrypi: $ ./rpi-test
Hello, world!
Chris Pick
  • 570
  • 4
  • 7