【问题标题】:How can I compile Rust code to run on a Raspberry Pi 2?如何编译 Rust 代码以在 Raspberry Pi 2 上运行?
【发布时间】:2015-04-28 10:58:39
【问题描述】:

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

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

我想要一个在 Raspberry Pi 2 上运行的与 Hello World 等效的 Rust 程序。它不必是字面意义上的 Hello World 程序,只要是具有类似低复杂度的程序即可。

【问题讨论】:

  • 您希望在 RPi 上运行 rustc,还是只想交叉编译将在 RPi 上执行的程序?
  • @Shepmaster 我想交叉编译程序以在 RPi2 上执行。我想在上面运行一些简单的东西。
  • @FilipeGonçalves RPi 与 RPi2 的兼容性如何?这似乎是一个不错的答案。
  • @DanielFath 老实说,我不知道。在快速谷歌搜索后,我想出了这些链接。如果有机会,我会在今天晚些时候尝试研究一下。

标签: rust raspberry-pi2


【解决方案1】:

我们现在有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

【讨论】:

  • 这工作得很好。我只需要添加缺少的引号转义 ("linker = \"arm-linux-gnueabihf-gcc\"")。您介意更新您的答案吗?
  • 这个答案适用于 Raspberry Pi 2s 和 3s,但不适用于 1s 或 Zeros。有关构建适用于所有 Pi 版本的二进制文件的设置,请参阅 my answer
  • 我必须在 Ubuntu 18.04 上使用 sudo apt install gcc-arm-linux-gnueabihf libc6-dev-armhf-cross libc6-dev-armhf-cross binutils-doc gcc-7-locales cpp-doc gcc-7-multilib-arm-linux-gnueabihf gcc-7-doc libgcc1-dbg-armhf-cross
【解决方案2】:

Rust 编译器不作为 Raspberry Pi 的交叉编译器分发,因此需要使用 rpi 开发工具作为交叉编译器进行编译。

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

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

  3. 在你家中寻找 rusty-pi 目录./configure --target=arm-unknown-linux-gnueabihf --prefix=$HOME/rusty-pi &amp;&amp; make &amp;&amp; make install

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

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

【讨论】:

    【解决方案3】:

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

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

    幸运的是,rustup 的 gcc-arm-linux-gnueabihf 目标 >= ARMv6(带有硬件浮点,所有 Raspberry Pi 都支持),所以只需要正确的链接器即可。 Raspberry Pi 基金会在其tools repository 中提供了其中之一。

    综上所述,以下步骤可用于交叉编译适用于所有 Raspberry Pi 的 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!
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多