When cross compiling for macOS, it's necessary to install the SDK (Software Development Kit) from Apple as well as a suitable compiler. The instructions for Windows (using MSYS2—described in the previous Appendix, Installation Details) and Linux are almost identical; the main work is to install the macOS SDK.
To prepare for cross compilation to Darwin, we must install the macOS SDK and a build toolchain that can use it. The easiest way to do this is with the osxcross project. This example shows how to download and install the SDK and tools to build for macOS without using a Macintosh computer. This illustration uses Linux but the process is the same for Windows developers using MSYS2 or Cygwin Command Prompts.
We'll be using clang
rather than gcc
as it's more portable by design. For this process to work, you'll need to have clang
, cmake
, and libxml2-dev
installed using your package manager:
- On Linux use:
pacman -S clang cmake libxml2-dev
(orapt-get
oryum
, depending on your distribution) - On Windows use:
pacman -S mingw-w64-x86_64-clang mingw-w64-x86_64-cmake mingw-w64-x86_64-libxml2
Next, we need to download the macOS SDK, which is bundled with Xcode. If you don't already have an Apple developer account, you'll need to sign up and agree to their terms and conditions. Using this account, log in to the download site at https://developer.apple.com/download/more/?name=Xcode%207.3 and download XCode.dmg
(7.3.1 is recommended for osxcross).
Then, we can install the osxcross tool—start by downloading it with git clone https://github.com/tpoechtrager/osxcross.git
and then change into the downloaded directory. Using these tools, we extract the macOS SDK from the downloaded Xcode.dmg
file using the package tool provided, ./tools/gen_sdk_package_darling_dmg.sh <path to Xcode.dmg>
. The resulting MacOSX10.11.sdk.tar.xz
file should be copied into the tarballs/
directory.
Lastly, we build the osxcross compiler extension by executing ./build.sh
. Following this, there should be a new directory named target/bin/
, which you should add to your PATH
environment variable. The compiler can now be used in CGo builds by setting the environment variable, CC=o32-clang
. More details about this process and how to adapt it for other platforms are available on the osxcross project website at https://github.com/tpoechtrager/osxcross.