Search code examples
macosrustx86-64cross-compiling

How can I cross compile Rust code into Intel assembly on an ARM M1 Apple Silicon Mac?


I've been comparing the assembly code generated by C and Rust for x86 and ARM.

I have an M1 Mac and I found how to cross-compile C with Clang, but so far I can't find how to cross-compile Rust.

How can I generate an x86_64 binary from Rust on an M1 Mac?


Solution

  • Cross-compilation is built in, just use rustup to install the target support:

    $ rustup target install x86_64-apple-darwin
    

    and build your crate like this:

    $ cargo build --target x86_64-apple-darwin
    

    Thanks to Rosetta you can even run it like this:

    $ cargo run --target x86_64-apple-darwin