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:

Mac

$ brew install arm-linux-gnueabihf-binutils
$ rustup target add armv7-unknown-linux-musleabihf
Enter fullscreen mode Exit fullscreen mode

In .cargo/config

[build]
target = "armv7-unknown-linux-musleabihf"
[target.armv7-unknown-linux-c]
linker = "arm-linux-gnueabihf-ld"
Enter fullscreen mode Exit fullscreen mode

With simple src/main.rs

fn main() {
    println!("Hello, Raspberry!");
}
Enter fullscreen mode Exit fullscreen mode

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@192.168.1.43:
pi@192.168.1.43's password: 
rpi                                                                                         100% 2702KB   2.6MB/s   00:01    
Hasans-Air:rpi hasan$ ssh pi@192.168.1.43 'chmod +x ~/rpi && ~/rpi'
pi@192.168.1.43's password: 
Hello, Raspberry!
Enter fullscreen mode Exit fullscreen mode

Win 10
Get the linker from here, and run:

rustup target add armv7-unknown-linux-gnueabihf
Enter fullscreen mode Exit fullscreen mode

Creating file .cargo/config with content:

[build]
target = "armv7-unknown-linux-gnueabihf"

[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"
Enter fullscreen mode Exit fullscreen mode

And with simple src/main.rs:

fn main() {
    println!("Hello, Raspberry! from Win 10");
}
Enter fullscreen mode Exit fullscreen mode

I was able to get things done

enter image description here

enter image description here

Top comments (1)

Collapse
 
valiice profile image
Valentin

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!