簡體   English   中英

如何編譯Rust代碼以在Raspberry Pi 2上運行?

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

我最近收購了一個Raspberry PI 2,我想在它上面運行Rust程序。

是否有指南/說明如何在Raspberry PI 2上交叉編譯Rust程序? 我聽說過在RPi或Arduino上運行Rust,雖然最近沒有。

我想在Raspberry Pi 2上運行一個Hello World等效的Rust程序。它不一定是文字的Hello World程序,只是具有類似低復雜度的東西。

我們現在已經生銹了。

$ 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

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. 在你的家里尋找生銹的pi目錄./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

它會產生一個可執行文件。

@ 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

測試交叉編譯器(假設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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM