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

前端 未结 3 1216
旧时难觅i
旧时难觅i 2021-01-30 17:54

I recently acquired a Raspberry PI 2 and I want to run a Rust program on it.

Is there a guide/instructions how to cross compile Rust programs on Raspberry PI 2? I\'ve h

相关标签:
3条回答
  • 2021-01-30 18:07

    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.

    1. Get rpi dev tools - git clone https://github.com/raspberrypi/tools.git ~/pi-tools

    2. 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

    3. Look for rusty-pi dir on your home ./configure --target=arm-unknown-linux-gnueabihf --prefix=$HOME/rusty-pi && make && make install

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

    It will produce an executable.

    0 讨论(0)
  • 2021-01-30 18:14

    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
    
    0 讨论(0)
  • 2021-01-30 18:24

    @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).

    The problem is that Debian/Ubuntu's armhf port (and thus their gcc-arm-linux-gnueabihf package/compiler/toolchain) targets >= 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. The Raspberry Pi foundation provides one of those in their tools repository.

    Putting it together, the following steps can be used to cross compile a Rust binary that works on all Raspberry Pis:

    $ 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):

    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!
    
    0 讨论(0)
提交回复
热议问题