How can I compile Rust code to run on a Raspberry Pi 2?
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
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.
Get rpi dev tools -
git clone https://github.com/raspberrypi/tools.git ~/pi-tools
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
Look for rusty-pi dir on your home
./configure --target=arm-unknown-linux-gnueabihf --prefix=$HOME/rusty-pi && make && make install
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.
@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!