如何编译Rust代码以在Raspberry Pi 2上运行?

时间:2015-04-28 10:58:40

标签: rust raspberry-pi2

我最近收购了一个Raspberry PI 2,我想在它上面运行一个Rust程序。

是否有指南/说明如何在Raspberry PI 2上交叉编译Rust程序?我听说过在RPi或Arduino上运行Rust,虽然最近没有。

我想在Raspberry Pi 2上运行一个Hello World等效的Rust程序。它不一定是文字的Hello World程序,只是具有类似低复杂度的东西。

3 个答案:

答案 0 :(得分:24)

我们现在有rustup

$ 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

答案 1 :(得分:9)

Rust编译器不是作为Raspberry Pi的交叉编译器分发的,因此需要使用rpi dev工具将其编译为交叉编译器。

  1. 获取rpi开发工具 - git clone https://github.com/raspberrypi/tools.git ~/pi-tools

  2. 从mozilla git repo获取生锈编译器并将rpi工具添加到路径export PATH=~/pi-tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin:$PATH

  3. 在您的家./configure --target=arm-unknown-linux-gnueabihf --prefix=$HOME/rusty-pi && make && make install

  4. 上寻找生锈的目录
  5. 考虑helloworld.rs - &gt; % ~/pi-rust/bin/rustc --target=arm-unknown-linux-gnueabihf -C linker=arm-linux-gnueabihf-g++ helloworld.rs

  6. 它将生成一个可执行文件。

答案 2 :(得分:6)

@kazhik's answer适用于Raspberry Pi 2s和3s(ARMv7/8 based),但不适用于Raspberry Pi 1s或Zeros(ARMv6 based)。

问题是Debian / Ubuntu的armhf端口(以及它们的gcc-arm-linux-gnueabihf包/编译器/工具链)目标是&gt; = ARMv7

幸运的是,rustup的gcc-arm-linux-gnueabihf目标&gt; = ARMv6(使用硬件浮点,所有Raspberry Pis都支持),所以所需要的只是正确的链接器。 Raspberry Pi基金会提供tools repository中的其中一个。

将它们放在一起,可以使用以下步骤交叉编译适用于所有Raspberry Pis的Rust二进制文件:

$ 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

测试交叉编译器(假设Pi正在运行且可以使用默认的raspberrypi主机名访问):

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!