Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross-compiling Rust from Windows to ARM Linux

I'm using Windows 10. I would like to cross-compile a Rust program to run on armv7-unknown-linux-gnueabihf. (armv7-unknown-linux-muscl would also be acceptable but it doesn't seem to be available.)

Here are my steps:

  1. Install rustup
  2. rustup toolchain install stable-x86_64-pc-windows-gnu
  3. rustup toolchain default stable-x86_64-pc-windows-gnu
  4. rustup target add armv7-unknown-linux-gnueabihf
  5. Edit my ./cargo/config file to contain:

    [build]
    target = "armv7-unknown-linux-gnueabihf"
    
  6. cargo build

This compiles everything fine, but when it comes to linking it gives this error:

error: could not exec the linker `cc`: The system cannot find the file specified. (os error 2)

As far as I have been able to determine, this is because Rust doesn't have its own linker and uses GCC instead. Apparently I need to provide this myself and add this to the ./cargo/config file:

[target.armv7-unknown-linux-gnueabihf]
linker = "c:/path/to/my/gcc/cross/compiler"

Is that right? If so where on Earth can I download such a cross-compiler for Windows and why doesn't rustup install it? Having to compile a cross-compiling version of GCC yourself is the biggest pain of cross-compiling C/C++ programs. Does Rustup really not make this any easier?

like image 736
Timmmm Avatar asked Sep 26 '16 14:09

Timmmm


People also ask

Can rust be cross compiled?

Rust supports a great number of platforms. For many of these platforms The Rust Project publishes binary releases of the standard library, and for some the full compiler.

How do I cross-compile gcc for arms?

Install gcc-arm-linux-gnueabi and binutils-arm-linux-gnueabi packages, and then just use arm-linux-gnueabi-gcc instead of gcc for compilation. This brings in the complete cross-compile environment, including binutils. On Ubuntu 13.10 you get gcc-4.7 for 'gnueabi' and gcc-4.8 for 'gnueabihf'.

What is Rustup target?

rustup is a toolchain multiplexer. It installs and manages many Rust toolchains and presents them all through a single set of tools installed to ~/. cargo/bin . The rustc and cargo executables installed in ~/. cargo/bin are proxies that delegate to the real toolchain.


1 Answers

For MacOS better use: musleabihf, for Windows you can use gnueabihf as bellow:

Mac

$ brew install arm-linux-gnueabihf-binutils
$ rustup target add armv7-unknown-linux-musleabihf

In .cargo/config

[build]
target = "armv7-unknown-linux-musleabihf"
[target.armv7-unknown-linux-c]
linker = "arm-linux-gnueabihf-ld"

With simple src/main.rs

fn main() {
    println!("Hello, Raspberry!");
}

Then things are fine:

Hasans-Air:rpi hasan$ cargo build
   Compiling rpi v0.1.0 (/Users/hasan/PycharmProjects/rpi)
    Finished dev [unoptimized + debuginfo] target(s) in 0.41s
Hasans-Air:rpi hasan$ scp target/armv7-unknown-linux-musleabihf/debug/rpi [email protected]:
[email protected]'s password: 
rpi                                                                                         100% 2702KB   2.6MB/s   00:01    
Hasans-Air:rpi hasan$ ssh [email protected] 'chmod +x ~/rpi && ~/rpi'
[email protected]'s password: 
Hello, Raspberry!

Win 10 Get the linker from here, and run:

rustup target add armv7-unknown-linux-gnueabihf

Creating file .cargo/config with content:

[build]
target = "armv7-unknown-linux-gnueabihf"

[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"

And with simple src/main.rs:

fn main() {
    println!("Hello, Raspberry! from Win 10");
}

I was able to get things done

enter image description here

enter image description here

like image 176
Hasan A Yousef Avatar answered Sep 26 '22 12:09

Hasan A Yousef