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

Dan*_*ath 27 rust raspberry-pi2

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

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

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

小智 24

我们现在已经生锈了.

$ 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
Run Code Online (Sandbox Code Playgroud)


dro*_*aak 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. 在你的家里寻找生锈的dir dir ./configure --target=arm-unknown-linux-gnueabihf --prefix=$HOME/rusty-pi && make && make install

  4. 考虑helloworld.rs - > % ~/pi-rust/bin/rustc --target=arm-unknown-linux-gnueabihf -C linker=arm-linux-gnueabihf-g++ helloworld.rs

它会产生一个可执行文件.


Chr*_*ick 7

@ kazhik的答案适用于Raspberry Pi 2s和3s(基于ARMv7/8),但不适用于Raspberry Pi 1s或Zeros(基于ARMv6).

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

幸运的是,rustup的gcc-arm-linux-gnueabihf目标> = ARMv6(具有硬件浮点,所有Raspberry Pis都支持),所以所需要的只是正确的链接器.Raspberry Pi基础提供了其工具库中的一个.

将它们组合在一起,可以使用以下步骤来交叉编译适用于所有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
Run Code Online (Sandbox Code Playgroud)

测试交叉编译器(假设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!
Run Code Online (Sandbox Code Playgroud)