[英]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).我正在寻找一种方法来编译 C++ 项目以在具有不同 macOS 版本(macOS 11 和 12)的 M1 上工作。
I looked at this question: Build Apple Silicon binary on Intel machine我看了这个问题: 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:它适用于 macOS 11,但不适用于 macOS 12。对于 macOS 12,还有其他目标:
-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)?是否有一个通用的-target
我可以使用它适用于所有 macOS 版本(甚至可能是未来版本)?
Actually, Xcode is a pretty good C++ compiler.实际上,Xcode 是一个相当不错的 C++ 编译器。 If you use Xcode to build C++, to build a universal binary.如果使用 Xcode 构建 C++,构建通用二进制。 You will find Xcode has below steps when building(remember to set "NO" to Build Active Architecture Only in build settings):您会发现 Xcode 在构建时有以下步骤(请记住在构建设置中将“NO”设置为仅构建活动架构): It first compiles and link for x86_64 and then arm64, and finally uses lipo
to create a universal binary.它首先为 x86_64 和 arm64 编译和链接,最后使用lipo
创建一个通用二进制文件。
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).因此,您可以创建一个演示 c++ 代码并检查构建报告以检查 xcode 使用什么命令来编译您的项目(通过折叠右侧的菜单图标)。
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)?是否有一个通用的 -target 我可以使用它适用于所有 macOS 版本(甚至可能是未来版本)?
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.例如,如果您的目标是 macos 11,则可以将-target x86_64-apple-macos11.0
用于英特尔 CPU, arm64-apple-macos11.0
用于苹果硅 CPU。 By default, targeting macos 11 means you can run the binary in future macos version.默认情况下,以 macOS 11 为目标意味着您可以在未来的 macOS 版本中运行二进制文件。
If you have any questions leave a comment below.如果您有任何问题,请在下面发表评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.