Cross-Compilation to X86_64-Unknown-Linux-Gnu Fails on MAC Osx

Cargo build for Linux compilation errors from Once_cell?

The error you're getting indicates that the standard library can't be found. When cross-compiling from Windows to Linux, it's best to use *-musl targets, to avoid having to come up with the Linux-specific libraries that *-gnu targets require. To fix the errors simply run the following command:

rustup target add x86_64-unknown-linux-musl

This will install the relevant stdlib. You will have to do this for each toolchain you intend to cross-compile from (if you want to use both stable and nightly or something).

If you cargo build at this point, and instead of getting a bunch of compiler errors, you should be getting linker errors instead. To fix this, add the following lines to your .cargo/config.toml file:

[target.x86_64-unknown-linux-musl]
linker = "rust-lld"

That should be enough to get things built, although some crates may have platform/toolchain requirements that might take some more effort to satisfy.

Of course, that assumes amazon's ec2 instances are actually x86_64 machines, which they may not be. Similar steps should work for other platforms, just using the required target. For example, to build for a 64-bit arm platform you would just use the same steps but with the target aarch64-unknown-linux-musl, and I use these steps with armv7-unknown-linux-musleabihf to build stuff for my raspberry pi.

How to cross compile from Mac to Linux?

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!

Compling Rust on Mac M1 for target x86_64 linux

It looks like the executable is actually named x86_64-linux-gnu-gcc, see https://packages.debian.org/bullseye/arm64/gcc-x86-64-linux-gnu/filelist.



Related Topics



Leave a reply



Submit