osxcross
OSXCross branches:
master
:
- Uses
cctools 986
andld64 711
. Known to be stable and well-working.
2.0-llvm-based
:
- OSXCross based on LLVM tools.
Easiest to build and recommended for new projects.
Does not support building fori386
.
testing
:
- Uses the latest versions of
cctools
andld64
— currentlycctools 1024.3
andld64 955.13
.
Takes the longest to build due to additional dependencies introduced in the latest versions.
ppc-test
:
- An older branch with support for PPC targets.
What is OSXCross
OSXCross provides a macOS cross-compilation toolchain.
Supported OSes and architectures
- Host OSes: Linux, *BSD
- Host architectures: x86, x86_64, ARM, AArch64/arm64
- Target architectures: arm64, arm64e, x86_64, i386
How It Works
macOS cross-compilation requires:
- Clang/LLVM (cross-compilation supported by default)
- A macOS SDK
This branch of OSXCross uses cctools-port.
cctools-port
provides toolchain tools such as ar
, lipo
, otool
, and ld64
as linker.
OSXCross provides a set of scripts for preparing the SDK, building cctools-port
(along with its dependencies), and compiling the compiler wrapper.
It also includes scripts for optionally building:
- Up-to-date LLVM tools and clang (
./build_clang.sh
,./build_apple_clang.sh
) - Vanilla GCC as a cross-compiler for target macOS (
./build_gcc.sh
- The "compiler-rt" runtime library (
./build_compiler_rt.sh
)
Package Manager
A minimal MacPorts package manager is included. See README.MACPORTS.md.
Installation
Place your packaged SDK in the tarballs/
directory.
Install the following dependencies:
clang cmake git patch python libssl-dev lzma-dev libxml2-dev xz bzip2 cpio libbz2 zlib1g-dev bash
Optional:
llvm-dev
: For Link Time Optimization support andld64
-bitcode_bundle
supportuuid-dev
: For ld64-random_uuid
support
You can run sudo tools/get_dependencies.sh
to get these (and the optional packages) automatically. (outdated)
Build Clang (Optional - if you need an up-to-date version of LLVM/Clang)
./build_clang.sh # Builds mainline Clang
./build_apple_clang.sh # Builds Apple's Clang
INSTALLPREFIX=/opt/clang ./build_clang.sh # Custom install path
Build OSXCross
By default, this installs the osxcross toolchain into <current-directory>/target
.
To specify a different installation path or run the build unattended,
set the TARGET_DIR
and/or UNATTENDED
environment variables accordingly.
./build.sh
[TARGET_DIR=/usr/local/osxcross] [OSX_VERSION_MIN=XX.X] [UNATTENDED=1] ./build.sh
Add <target>/bin
to your PATH
after installation.
Build GCC (Optional)
./build_gcc.sh
GCC_VERSION=5.2.0 ENABLE_FORTRAN=1 ./build_gcc.sh
Install GCC dependencies:
sudo apt-get install gcc g++ zlib1g-dev libmpc-dev libmpfr-dev libgmp-dev
Notes:
- To enable
-Werror=implicit-function-declaration
, setOSXCROSS_ENABLE_WERROR_IMPLICIT_FUNCTION_DECLARATION=1
- To disable static linking:
OSXCROSS_GCC_NO_STATIC_RUNTIME=1
*-g++-libc++
uses Clang's libc++ — only use if needed
Packaging the SDK
Please ensure you have read and understood the Xcode license terms before continuing.
SDKs can be extracted either from the full Xcode or from the Xcode Command Line Tools.
On macOS
From Full Xcode
- Download Xcode
- Mount
Xcode.dmg
(Right-click → Open With → DiskImageMounter)- If you see a crossed-circle dialog when mounting, ignore it — installation of Xcode is not required
- Run:
./tools/gen_sdk_package.sh
(from the OSXCross package) - Copy the resulting SDK (
*.tar.*
or*.pkg
) to a USB stick - On Linux/BSD, move the SDK to the
tarballs/
directory of OSXCross
From Command Line Tools
- Download Command Line Tools
- Mount the
Command_Line_Tools_for_Xcode.dmg
(Open With → DiskImageMounter) - Install
Command Line Tools.pkg
(Open With → Installer) - Run:
./tools/gen_sdk_package_tools.sh
- Copy the resulting SDK (
*.tar.*
or*.pkg
) to a USB stick - On Linux/BSD, move the SDK to the
tarballs/
directory of OSXCross
On Linux (and others)
Method 1 (Xcode > 8.0)\ Requires up to 45 GB free disk space. SSD strongly recommended.
- Download Xcode as described above
- Install:
clang
,make
,libssl-devel
,lzma-devel
, andlibxml2-devel
- Run:
./tools/gen_sdk_package_pbzx.sh <xcode>.xip
- Move the SDK to the
tarballs/
directory
Method 2 (up to Xcode 7.3)
- Download Xcode as described above
- Install:
cmake
,libxml2-dev
, andfuse
- Run:
./tools/gen_sdk_package_darling_dmg.sh <xcode>.dmg
- Move the SDK to the
tarballs/
directory
Method 3 (up to Xcode 7.2)
- Download Xcode as described above
- Ensure
clang
andmake
are installed - Run:
./tools/gen_sdk_package_p7zip.sh <xcode>.dmg
- Move the SDK to the
tarballs/
directory
Method 4 (Xcode 4.2)
- Download Xcode 4.2 for Snow Leopard (ensure it's the correct version)
- Install
dmg2img
- As root, run:
./tools/mount_xcode_image.sh /path/to/xcode.dmg
- Follow the on-screen instructions from the script
- Move the SDK to the
tarballs/
directory
From Xcode Command Line Tools
- Download Command Line Tools as described above
- Install:
clang
,make
,libssl-devel
,lzma-devel
, andlibxml2-devel
- Run:
./tools/gen_sdk_package_tools_dmg.sh <command_line_tools_for_xcode>.dmg
- Move the SDK to the
tarballs/
directory
Usage Examples
Compile test.cpp
- i386:
i386-apple-darwinXX-clang++ test.cpp -O3 -o test
(if your SDK supportsi386
) - x86_64:
x86_64-apple-darwinXX-clang++ test.cpp -O3 -o test
- arm64:
arm64-apple-darwinXX-clang++ test.cpp -O3 -o test
- arm64e:
arm64e-apple-darwinXX-clang++ test.cpp -O3 -o test
Or by using xcrun:
- i386:
xcrun clang++ -arch x86_64 test.cpp -O3 -o test
- x86_64:
xcrun clang++ -arch x86_64 test.cpp -O3 -o test
- arm64:
xcrun clang++ -arch arm64 test.cpp -O3 -o test
- arm64e:
xcrun clang++ -arch arm64e test.cpp -O3 -o test
GCC: Replace clang++
with g++
as needed
Check your target version with osxcross-conf
, see TARGET
.
Build Makefile project
xcrun -f clang
prints the path to clang.
make CC=$(xcrun -f clang) CXX=$(xcrun -f clang++)
Build autotools project
CC=x86_64-apple-darwinXX-clang CXX=x86_64-apple-darwinXX-clang++ ./configure --host=x86_64-apple-darwinXX
libc++ Example (macOS 10.7+ required)
xcrun clang++ -stdlib=libc++ -std=c++11 test.cpp -o test
Shortcut:
x86_64-apple-darwinXX-clang++-libc++ -std=c++11 test.cpp -o test
LTO Example
xcrun clang++ test1.cpp -O3 -flto -c
xcrun clang++ test2.cpp -O3 -flto -c
xcrun clang++ -O3 -flto test1.o test2.o -o test
Universal Binary
xcrun clang++ test.cpp -O3 -arch x86_64 -arch arm64 -o test
GCC:
xcrun g++ -arch x86_64 test.cpp -O3 -o test.x86_64
xcrun g++ -arch arm64 test.cpp -O3 -o test.arm64
xcrun lipo -create test.x86_64 test.arm64 -output test
Deployment Target
Default:
- SDK ≤ 10.13 → macOS 10.6
- SDK ≥ 10.14 → macOS 10.9
- SDK ≥ 14.0 → macOS 10.13
Can be overriden via:
- During the build:
OSX_VERSION_MIN=XX.X ./build.sh
. - By passing
-mmacos-version-min=XX.X
to the compiler. - By setting
MACOSX_DEPLOYMENT_TARGET=XX.X
env var.
Note: Deployment target ≥ 10.9 defaults to libc++
.
Can be explicitely overriden by setting the C++ library to libstdc++
via -stdlib=libstdc++
.
Projects Using OSXCross
- multiarch/crossbuild:
various cross-compilers
(Systems: Linux, macOS, Windows, Archs: x86_64,i386, arm, ppc, mips)
in Docker. OSXCross powers the Darwin builds. - Smartmontools
License
scripts/wrapper
: GPLv2cctools/ld64
: APSL 2.0 (legacy)xar
: New BSD