Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

macOS build universal binary 2 with CMake

Tags:

c++

macos

cmake

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!

like image 938
theBanana19 Avatar asked Dec 05 '20 13:12

theBanana19


1 Answers

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
like image 138
Dmitry Avatar answered Oct 13 '22 14:10

Dmitry