[英]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. 我最近收购了一个Raspberry PI 2,我想在它上面运行Rust程序。
Is there a guide/instructions how to cross compile Rust programs on Raspberry PI 2? 是否有指南/说明如何在Raspberry PI 2上交叉编译Rust程序? I've heard about running Rust on RPi or Arduino, although not recently.
我听说过在RPi或Arduino上运行Rust,虽然最近没有。
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. 我想在Raspberry Pi 2上运行一个
Hello World
等效的Rust程序。它不一定是文字的Hello World程序,只是具有类似低复杂度的东西。
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
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. Rust编译器不作为Raspberry Pi的交叉编译器分发,因此需要使用rpi dev工具将其编译为交叉编译器。
Get rpi dev tools - git clone https://github.com/raspberrypi/tools.git ~/pi-tools
获取rpi开发工具 -
git clone https://github.com/raspberrypi/tools.git ~/pi-tools
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
从mozilla git repo获取生锈编译器并将rpi工具添加到路径
export PATH=~/pi-tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin:$PATH
Look for rusty-pi dir on your home ./configure --target=arm-unknown-linux-gnueabihf --prefix=$HOME/rusty-pi && make && make install
在你的家里寻找生锈的pi目录
./configure --target=arm-unknown-linux-gnueabihf --prefix=$HOME/rusty-pi && make && make install
Considering helloworld.rs -> % ~/pi-rust/bin/rustc --target=arm-unknown-linux-gnueabihf -C linker=arm-linux-gnueabihf-g++ helloworld.rs
考虑helloworld.rs - >
% ~/pi-rust/bin/rustc --target=arm-unknown-linux-gnueabihf -C linker=arm-linux-gnueabihf-g++ helloworld.rs
It will produce an executable. 它会产生一个可执行文件。
@kazhik's answer will work for Raspberry Pi 2s and 3s (which are ARMv7/8 based ), but not for Raspberry Pi 1s or Zeros (which are ARMv6 based ). @ kazhik的答案适用于Raspberry Pi 2s和3s( 基于ARMv7 / 8 ),但不适用于Raspberry Pi 1s或Zeros( 基于ARMv6 )。
The problem is that Debian/Ubuntu's armhf
port (and thus their gcc-arm-linux-gnueabihf
package/compiler/toolchain) targets >= ARMv7 . 问题是Debian / Ubuntu的
armhf
端口(以及它们的gcc-arm-linux-gnueabihf
软件包/编译器/工具链)目标是> = ARMv7 。
Fortunately, rustup's gcc-arm-linux-gnueabihf
targets >= ARMv6 (with hardware floating-point, which all Raspberry Pis support), so all that's needed is the correct linker. 幸运的是,rustup的
gcc-arm-linux-gnueabihf
目标> = ARMv6 (具有硬件浮点,所有Raspberry Pis都支持),因此所需要的只是正确的链接器。 The Raspberry Pi foundation provides one of those in their tools repository . Raspberry Pi基础提供了其工具库中的一个 。
Putting it together, the following steps can be used to cross compile a Rust binary that works on all Raspberry Pis: 将它们放在一起,可以使用以下步骤交叉编译适用于所有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
To test the cross-compiler (assuming a Pi is running and reachable with the default raspberrypi
hostname): 测试交叉编译器(假设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!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.