Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cross compile from Mac to Linux?

I wrote a little game using Rust, and I used cargo build --release to compile a release version on Mac.

I tried to share this with my friend who is using Ubuntu, but when he tried to run the binary, he got the following error:

cannot execute binary file: Exec format error

I searched for this but found no answers. Doesn't Rust claim to have "no runtime"? Shouldn't it be able to run anywhere in binary form?

like image 375
FrontMage Avatar asked Jan 20 '17 10:01

FrontMage


People also ask

What is GCC cross compiler?

Generally speaking, a cross-compiler is a compiler that runs on platform A (the host), but generates executables for platform B (the target). These two platforms may (but do not need to) differ in CPU, operating system, and/or executable format.


1 Answers

Rust not having a runtime means that it doesn't have a lot of code running as part of the language (for example a garbage collector or bytecode interpreter). It does still need to use operating system primitives (i.e. syscalls), and these are different on MacOS and Linux.

What you want is a cross compiler. If you're using rustup, then installing a cross compiler should be simple:

# Install the toolchain to build Linux x86_64 binaries
rustup target add x86_64-unknown-linux-gnu

Then building is:

cargo build --release --target=x86_64-unknown-linux-gnu

Caveat: I don't have an OS X machine to test this on; please comment or edit to fix this if it works!

like image 193
Chris Emerson Avatar answered Sep 22 '22 13:09

Chris Emerson