Jeremy gave a good answer, but I'd like to add a few things and offer my two cents.
The updated script
#!/bin/bash
OPT_FLAGS="-O3 -g3"
MAKE_JOBS=8
dobuild() {
export CC
CC=$(xcrun --find --sdk "${SDK}" gcc)
export CXX
CXX=$(xcrun --find --sdk "${SDK}" g++)
export CPP
CPP=$(xcrun --find --sdk "${SDK}" cpp)
export CFLAGS
CFLAGS="${HOST_FLAGS} ${OPT_FLAGS}"
export CXXFLAGS
CXXFLAGS="${HOST_FLAGS} ${OPT_FLAGS}"
export LDFLAGS
LDFLAGS="${HOST_FLAGS}"
./configure --host="${CHOST}" --prefix="${PREFIX}" --enable-static
make clean
make -j"${MAKE_JOBS}"
make install
}
SDK="iphoneos"
ARCH_FLAGS="-arch armv7 -arch armv7s -arch arm64"
HOST_FLAGS="${ARCH_FLAGS} -miphoneos-version-min=8.0 -isysroot $(xcrun --sdk ${SDK} --show-sdk-path)"
CHOST="arm-apple-darwin"
PREFIX="${HOME}/DEVICE_ARM"
dobuild
SDK="iphonesimulator"
ARCH_FLAGS="-arch x86_64"
HOST_FLAGS="${ARCH_FLAGS} -mios-simulator-version-min=8.0 -isysroot $(xcrun --sdk ${SDK} --show-sdk-path)"
CHOST="x86_64-apple-darwin"
PREFIX="${HOME}/SIM_x86"
dobuild
The iOS toolchain is separated by SDKs and iphonesimulator has a separate SDK as does macos, tvos, and of course iphoneos. For each SDK you'll need one compile. You can lipo together iphonesimulator and iphoneos output into a single library since they contain different architectures, but really they are not compiled with the same SDK. I recommend against the super binary of mixed SDKs.
Why am I doing this?
If you're compiling a library and need this, it is because the library is using autoconf, or in the case of Boost and OpenSSL other custom build systems. The keys to compiling for the SDKs are the correct clang
, -sysroot
, -miphoneos-ver-min
, and -arch
flags. If you get the -arch
or -sysroot
flags wrong you'll see #error Unsupported architecture
errors.
Remove the --disable-shared
flag
I like leaving the generation of the shared libs even if I intend to use the static library. This generally means you'll compile with Position Independent Code (-fPIC) so if you decide to include this library in a shared library, you're good to go. Also, unlike a static library which isn't linked, but archived, the linking of the shared library often exposes missing objects.
Multiple architectures on the line
You can pass multiple -arch
flags on the line and you'll get FAT binaries. This save you some hassle and time during compilation.
$ lipo -info libwhatever.a
Architectures in the fat file: libwhatver.a are: i386 x86_64
Remove architectures
In all honesty, do you need i386
support? If you're not targeting that device then don't include the architecture. You only need i386 if your host system runs an OS predating Lion.