Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compile Rust code to run on a Raspberry Pi 2?

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.

like image 876
Daniel Fath Avatar asked Apr 28 '15 10:04

Daniel Fath


People also ask

How do I run Rust on Raspberry Pi?

Installing Rust on Raspberry Pi Step 1: Open the terminal of Raspberry Pi Os, then write the following command to download Rust. Step 2: You have to wait till the process is completed. Then enter option 1 to proceed with the installation process. Step 3: You have to wait till the installation is completed.

Can Rust be used on Raspberry Pi?

rust_gpizero uses the Rust programming language to do physical computing on the Raspberry Pi. If you own a Raspberry Pi, chances are you may already have experimented with physical computing—writing code to interact with the real, physical world, like blinking some LEDs or controlling a servo motor.

How do I compile a Raspberry Pi code?

For compiling C programs, we use a compiler called gcc because it comes installed on the Raspberry Pi. It has many complicated options which allow us to tailor its output to our needs, however to compile a simple program like the Hello Pi above, it is nicely simple.

Can Rust be cross compiled?

Rust supports a great number of platforms. For many of these platforms The Rust Project publishes binary releases of the standard library, and for some the full compiler. rustup gives easy access to all of them.


2 Answers

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 
like image 96
kazhik Avatar answered Sep 22 '22 15:09

kazhik


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.

like image 30
druuu Avatar answered Sep 23 '22 15:09

druuu