DEV Community

Hasan Yousef
Hasan Yousef

Posted on

Cross compiling Rust for Raspberry Pi

In a previous post, I mentioned how to install Rust at RPi, this may not be accepted in order to save space in RPi and to get things done faster, the solution is to do cross compiler, that is compile in a given OS, and run in different OS, which is supported smoothly by Rust, what you need is a linker.

For compiling to RPi the linker required is arm-linux-gnueabihf-gcc which can be installed easily in Linux sudo apt-get install gcc-arm-linux-gnueabi make git-core ncurses-dev you can check this but it took half day from me to find how to do it in MacOS and Win 10, and here I'm sharing the final steps with you:

For MacOS better use: musleabihf as target, and for Windows you can use gnueabihf as bellow:


$ brew install arm-linux-gnueabihf-binutils
$ rustup target add armv7-unknown-linux-musleabihf

In .cargo/config

target = "armv7-unknown-linux-musleabihf"
linker = "arm-linux-gnueabihf-ld"

With simple src/

fn main() {
    println!("Hello, Raspberry!");

Then things are fine:

Hasans-Air:rpi hasan$ cargo build
   Compiling rpi v0.1.0 (/Users/hasan/PycharmProjects/rpi)
    Finished dev [unoptimized + debuginfo] target(s) in 0.41s
Hasans-Air:rpi hasan$ scp target/armv7-unknown-linux-musleabihf/debug/rpi pi@
pi@'s password: 
rpi                                                                                         100% 2702KB   2.6MB/s   00:01    
Hasans-Air:rpi hasan$ ssh pi@ 'chmod +x ~/rpi && ~/rpi'
pi@'s password: 
Hello, Raspberry!

Win 10
Get the linker from here, and run:

rustup target add armv7-unknown-linux-gnueabihf

Creating file .cargo/config with content:

target = "armv7-unknown-linux-gnueabihf"

linker = "arm-linux-gnueabihf-gcc"

And with simple src/

fn main() {
    println!("Hello, Raspberry! from Win 10");

I was able to get things done

enter image description here

enter image description here

Top comments (1)

valiice profile image

Thank you so much. I've been looking for the solution for half a day aswell. Your tutorial helped me in a matter of minutes!