I have just started using Rust and find it to be an exciting language. I am compiling code on Mac M1 laptop. However, the executables created are not running on Apple Silicone Macs, though they are running well on M1 and Intel macs. Where is the problem and how can it be solved? Thanks for your help.
This isn't meaningful. M1 is an Apple Silicon CPU. (It's like saying that your x86 code doesn't run on Intel.) Specifically what CPU doesn't your executable run on?
Can you show us the exact commands you have been running and any output that has been printed to the terminal?
Also, if you run rustc --version --verbose
it'll print out a bunch of information including your laptop's "target triple". That'll tell us what type of architecture and OS the Rust compiler is compiling for.
I'm using Rust on an M1 Mac (i.e. apple silicon) right now and haven't had any issues, so the issue might be related to the way Rust was installed or the commands you are running.
I just use command: "cargo build --release"
Okay, so how do you know it won't run? Can you copy/paste all the commands you execute while building the program and running it, as well as the output you get from each command?
There's a good chance all the information people need to troubleshoot this will be printed to the terminal, but by saying "I just use command: cargo build --release" we don't really have much to go on other than "it doesn't work".
You also didn't show the output from rustc --version --verbose
.
I run following commands:
cargo new myappl
cd myappl
cargo add dependency_pkg
cargo build
cargo run
I just edit the main.rs file till there are no errors and warnings and the output is ok. Then I just run the command "cargo build --release" and the executable file is built. I find it very elegant.
The output of rustc command is as follows:
$ rustc --version --verbose
rustc 1.68.2 (9eb3afe9e 2023-03-27)
binary: rustc
commit-hash: 9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0
commit-date: 2023-03-27
host: aarch64-apple-darwin
release: 1.68.2
LLVM version: 15.0.6
I copy the executable to other Macs and try to run them by double-clicking the file. Surprisingly, the executables created by above system runs well on Intel macs but not on AppleSilicon macs.
Correction: my mac is MacBook Air Apple M2 and not M1
Usually the problem is that when you migrated macOS from an Intel Mac, you have Intel-only Rust installation migrated, running under Rosetta. But your rustc --version
looks okay, and is seems to be aware of Apple Silicon (Rust calls it aarch64
).
To be 100% sure, you can run cargo build --target aarch64-apple-darwin --release
and it will build ARM-only executables in target/aarch64-apple-darwin/release
.
Note that Cargo doesn't support making Universal Binaries. To make one, you need lipo
command from Xcode:
cargo build --target aarch64-apple-darwin
cargo build --target x86_64-apple-darwin
lipo -create target/aarch64-apple-darwin/release/yourexe target/x86_64-apple-darwin/release/yourexe -output yourexe
Yes, it's really surprising. But the real surprise here is that it runs on Intel Mac.
Code which is not specifically signed for the distribution is not supposed to be usable on devices other than where it was built. This is just how macOS works novadays and has nothing to do with Rust, specifically.
P.S. It's not impossible to run unsigned software (or software signed for other Mac), but by default it's forbidden. You can google about ways to disable these checks, they are not that hard to find. And they are not Rust-specific at all.
@kornel : Thanks for very useful commands. They worked well on my system. I will be trying the newly created universal binary on other Macs tomorrow and will post feedback here.
@khimru : If universal binary does not work, I will try to disable the checks.
The universal binary created by lipo command works very well on both types of Mac. Thanks for your help.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.