简体   繁体   中英

Build Apple Silicon M1 binary on an Intel machine for different macOS versions

I'm looking for a way to compile a C++ project to work on M1 with different macOS versions (both macOS 11 and 12).

I looked at this question: Build Apple Silicon binary on Intel machine

And it suggests using:

-target arm64-apple-macos11

It works well for macOS 11 but doesn't work for macOS 12. For macOS 12 there are other targets:

-target arm64-apple-macos12

-target arm64-apple-macos12.3

Is there a generic -target I can use that will work for all macOS versions (maybe even future versions)?

Actually, Xcode is a pretty good C++ compiler. If you use Xcode to build C++, to build a universal binary. You will find Xcode has below steps when building(remember to set "NO" to Build Active Architecture Only in build settings): 在此处输入图像描述 It first compiles and link for x86_64 and then arm64, and finally uses lipo to create a universal binary.

So you can create a demo c++ code and inspect the build reports to check what command xcode is using to compile your project(by collapsing the menu icon on the right side).

So get back to your question:

Is there a generic -target I can use that will work for all macOS versions (maybe even future versions)?

Yes. For example, if you are targeting macos 11, you can use -target x86_64-apple-macos11.0 for intel CPUs and arm64-apple-macos11.0 for apple silicon CPUs. By default, targeting macos 11 means you can run the binary in future macos version.

If you have any questions leave a comment below.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM