Compiling code for Apple Silicon / M1 / ARM
Small projects/single files
$ joe helloworld.c $ CC="/usr/bin/cc" CFLAGS="-target arm64-apple-macos11" cc helloworld.c -o helloworld
Bigger projects with autoconf
The issue: Standard ./configure / make will produce x86_64 binaries. We want ARM64 for Apple M1, Big Sur / macOS 11.0.1, XCode 12.2, Apple clang version 12.0.0 (clang-1200.0.32.27)
$ cd src/ngircd-26 $ make distclean $ CC="/usr/bin/cc" CFLAGS="-target arm64-apple-macos11" ./configure --host=aarch64-apple-darwin --target=aarch64-apple-darwin --build=aarch64-apple-darwin […]
The trick is, that we both convince configure and manually enforce compiler-choice and flags:
- CC="/usr/bin/cc"
- We want clang/LLVM, not gcc
- CFLAGS="-target arm64-apple-macos11"
- We want that target archicture for the C compiler
- configure --host/--target/--build=aarch64-apple-darwin
- autoconf should generate for these architecture choices. Note that the syntax/name for the arch is different here!
[…] $ make -j8 $ file src/ngircd/ngircd src/ngircd/ngircd: Mach-O 64-bit executable arm64 $ src/ngircd/ngircd --version ngIRCd 26-IRCPLUS+SYSLOG+ZLIB-aarch64/apple/darwin
Job done, we have M1/ARM64 binaries!