Cross-compiling Rust programs for Raspbian

I just figured out how to cross-compile Rust programs for Raspberry Pis running Rasbian Stretch. Host system is an amd64 laptop running Debian unstable.

Turns out I was using the wrong target all along. For reference, here's a uname -a on the Pi:

Linux pi 4.9.41+ #1023 Tue Aug 8 15:47:12 BST 2017 armv6l GNU/Linux
  1. Install compilers:

    apt-get install gcc-arm-linux-gnueabi g++-arm-linux-gnueabi

    Without those packages installed, you'll run into "configure: error: C compiler cannot create executables".

  2. Configure cargo to use the correct linker when invoking rustc. Put the following into ~/.cargo/config:

    linker = "arm-linux-gnueabi-gcc"

    Without this, rustc will fill your console with "Relocations in generic ELF (EM: 40)" errors.

  3. Install the toolchain:

    rustup  target add arm-unknown-linux-gnueabi
  4. Cd into whatever project you'd like to build and run:

    cargo build --target=arm-unknown-linux-gnueabi
  5. You should now have a target/arm-unknown-linux-gnueabi/debug directory which contains the binary you're looking for:

    target/arm-unknown-linux-gnueabi/debug/netio: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/, for GNU/Linux 3.2.0, BuildID[sha1]=823fc0735e12c488b313d820679e1b0006f90c3b, with debug_info, not stripped