10

How would I build a Universal Binary 2 that supports both Intel and Apple Silicon using CMake/Make? I have found some documentation here - https://developer.apple.com/documentation/xcode/building_a_universal_macos_binary - but that uses XCode, which i'm not using in my project. Thanks!

theBanana19
  • 101
  • 1
  • 3
  • That docs seems to talk about how to do it. I don't know cmake, but if you build a Makefile, it pretty much walks you through it. You have to build separate binaries for each target app then use the lipo tool to merge them. The doc you linked talks about all of it. – Joseph Larson Dec 05 '20 at 15:35

1 Answers1

22

To create a universal binary set the following variable

CMAKE_OSX_ARCHITECTURES=arm64;x86_64

If you use CMake GUI press "Add Entry" and then set Name to CMAKE_OSX_ARCHITECTURES, Type=String, Value=arm64;x86_64

Then Configure->Generate->make. Here is the output you should see after building (my exe name is sprint_5)

>> file sprint_5          
sprint_5: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit executable x86_64] [arm64:Mach-O 64-bit executable arm64]
sprint_5 (for architecture x86_64): Mach-O 64-bit executable x86_64
sprint_5 (for architecture arm64):  Mach-O 64-bit executable arm64
Dmitry
  • 1,912
  • 2
  • 18
  • 29
  • The question is: How do you conditionally add this only when configuring on an Apple Silicon system? Trying this on an Intel system is going to fail, I assume. – Oscar Aug 22 '22 at 06:42
  • 1
    @Oscar the following cmake discourse threads have some good related discussion: [thread 1](https://discourse.cmake.org/t/how-to-determine-which-architectures-are-available-apple-m1/2401/12), [thread 2](https://discourse.cmake.org/t/looking-for-someone-willing-verify-a-minimal-xcode-project-on-apple-silicon/2415) – starball Aug 31 '22 at 02:24
  • 4
    In order to set it in the `CMakeLists.txt` you must declare the variable **before** `project()` and it is a cache variable so it is set this way: `set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64" CACHE STRING "" FORCE)` – yan Oct 05 '22 at 17:37
  • 1
    how to add this variable on command line? – Nicholas Jela Dec 19 '22 at 03:11