简体   繁体   English

如何为英特尔和 arm 架构构建具有不同代码的 osx 通用二进制文件

[英]How to build an osx universal binary with different code for intel and arm architecture

My goal is to create a universal/fat binary of my app, my understanding is that means xcode will create an intel/x86_64 build and an M1/arm build and package them together.我的目标是创建我的应用程序的通用/胖二进制文件,我的理解是这意味着 xcode 将创建一个 intel/x86_64 构建和一个 M1/arm 构建以及 package 它们一起。 My code uses intel intrinsics which I can replace with NEON in the arm build but to do so I need a way to create a conditional block at compile time for the target architecture.我的代码使用了英特尔内在函数,我可以在 arm 构建中将其替换为 NEON,但为此我需要一种在编译时为目标架构创建条件块的方法。 Is this possible?这可能吗? How can I achieve this?我怎样才能做到这一点?

What I've tried so far:到目前为止我已经尝试过:

#include "TargetConditionals.h"
#if defined TARGET_CPU_ARM64
    #include <sse2neon.h> //NEON intristics
#endif
#if defined TARGET_CPU_X86_64
    #include <emmintrin.h> //Intel intristics
#endif

When this was used Xcode tried to compile the arm code into the x86_64 build which failed.当使用它时,Xcode 试图将 arm 代码编译到 x86_64 版本中,但失败了。 I suppose both targets are defined at compile time but Xcode builds intel then arm objects separately so there must be a way for it to define which is which that I can use.我想这两个目标都是在编译时定义的,但是 Xcode 分别构建 intel 然后 arm 对象,所以必须有一种方法来定义我可以使用哪个。

I found the solution, with help from the comments (thank you!)我在评论的帮助下找到了解决方案(谢谢!)

I was misusing the definitions, all these are defined in the header but only the relevant ones are set to 1, changing my "#ifdef x" to "#if x == 1" was the solution.我误用了定义,所有这些都在 header 中定义,但只有相关的设置为 1,将我的“#ifdef x”更改为“#if x == 1”是解决方案。

#if (TARGET_CPU_ARM64 == 1)
    #include <sse2neon.h>
#elif (TARGET_CPU_X86_64 == 1)
    #include <emmintrin.h>
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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