Home > Mobile >  How to build an osx universal binary with different code for intel and arm architecture
How to build an osx universal binary with different code for intel and arm architecture

Time:11-28

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. 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. 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. 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.

CodePudding user response:

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.

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