The Apple M1 chip is based on the ARM instruction set. To compile for the M1, add the target flag to the llvm compiler

				
				
					-target arm64-apple-macos11 -mmacosx-version-min=11.0				
			

To compile for x86_64 on the Apple M1 use the following flag

				
				
					-arch x86_64				
			
				
				
					#if arch(arm64)
   // Code meant for the arm64 architecture here.
#elseif arch(x86_64)
   // Code meant for the x86_64 architecture here.
#endif				
			

To see the architectures present in a built executable file, run the lipo command-line tool

				
				
					lipo -archs executable				
			

To generate an universal binary, use the lipo command-line tool

				
				
					lipo -create -output universal_executable x86_executable arm_executable				
			

Note that the same command applies to generate universal static libraries.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top
X