从理论上讲,我们似乎可以构建一个包含模拟器、iPhone和iPad的单一静态库。

然而,苹果没有我能找到的关于这方面的文档,Xcode的默认模板也没有配置这样做。

我正在寻找一个简单的,可移植的,可重用的技术,可以在Xcode中完成。

一些历史:

在2008年,我们曾经能够制作包含sim卡和设备的单个静态库。苹果禁用了这个功能。 整个2009年,我们做了一对静态库-一个用于sim,一个用于设备。苹果现在也禁用了这个功能。

引用:

This is a great idea, it's an excellent approach, but it doesn't work: http://www.drobnik.com/touch/2010/04/universal-static-libraries/ There's some bugs in his script that means it only works on his machine - he should be using BUILT_PRODUCTS_DIR and/or BUILD_DIR instead of "guesstimating" them) Apple's latest Xcode prevents you from doing what he's done - it simply will not work, due to the (Documented) change in how Xcode processes targets) Another SO questioner asked how to do it WITHOUT xcode, and with responses that focussed on the arm6 vs arm7 part - but ignored the i386 part: How do i compile a static library (fat) for armv6, armv7 and i386 Since Apple's latest changes, the Simulator part isn't the same as the arm6/arm7 difference any more - it's a different problem, see above)


当前回答

我把它做成了一个Xcode 4模板,与Karl的静态框架模板相同。

我发现构建静态框架(而不是普通的静态库)会导致LLVM随机崩溃,这是由于一个明显的链接器错误——所以,我猜静态库仍然有用!

其他回答

我花了很多时间试图构建一个可以在armv7、armv7s和模拟器上工作的大型静态库。终于找到了解决办法。

要点是分别构建两个库(一个用于设备,另一个用于模拟器),重命名它们以彼此区分,然后将它们创建到一个库中。

lipo -create libPhone.a libSimulator.a -output libUniversal.a

我试过了,很管用!

有一个命令行实用程序xcodebuild,你可以在xcode中运行shell命令。 因此,如果您不介意使用自定义脚本,这个脚本可能会帮助您。

#Configurations.
#This script designed for Mac OS X command-line, so does not use Xcode build variables.
#But you can use it freely if you want.

TARGET=sns
ACTION="clean build"
FILE_NAME=libsns.a

DEVICE=iphoneos3.2
SIMULATOR=iphonesimulator3.2






#Build for all platforms/configurations.

xcodebuild -configuration Debug -target ${TARGET} -sdk ${DEVICE} ${ACTION} RUN_CLANG_STATIC_ANALYZER=NO
xcodebuild -configuration Debug -target ${TARGET} -sdk ${SIMULATOR} ${ACTION} RUN_CLANG_STATIC_ANALYZER=NO
xcodebuild -configuration Release -target ${TARGET} -sdk ${DEVICE} ${ACTION} RUN_CLANG_STATIC_ANALYZER=NO
xcodebuild -configuration Release -target ${TARGET} -sdk ${SIMULATOR} ${ACTION} RUN_CLANG_STATIC_ANALYZER=NO







#Merge all platform binaries as a fat binary for each configurations.

DEBUG_DEVICE_DIR=${SYMROOT}/Debug-iphoneos
DEBUG_SIMULATOR_DIR=${SYMROOT}/Debug-iphonesimulator
DEBUG_UNIVERSAL_DIR=${SYMROOT}/Debug-universal

RELEASE_DEVICE_DIR=${SYMROOT}/Release-iphoneos
RELEASE_SIMULATOR_DIR=${SYMROOT}/Release-iphonesimulator
RELEASE_UNIVERSAL_DIR=${SYMROOT}/Release-universal

rm -rf "${DEBUG_UNIVERSAL_DIR}"
rm -rf "${RELEASE_UNIVERSAL_DIR}"
mkdir "${DEBUG_UNIVERSAL_DIR}"
mkdir "${RELEASE_UNIVERSAL_DIR}"

lipo -create -output "${DEBUG_UNIVERSAL_DIR}/${FILE_NAME}" "${DEBUG_DEVICE_DIR}/${FILE_NAME}" "${DEBUG_SIMULATOR_DIR}/${FILE_NAME}"
lipo -create -output "${RELEASE_UNIVERSAL_DIR}/${FILE_NAME}" "${RELEASE_DEVICE_DIR}/${FILE_NAME}" "${RELEASE_SIMULATOR_DIR}/${FILE_NAME}"

可能看起来效率不高(我不擅长shell脚本),但很容易理解。 我配置了一个只运行此脚本的新目标。脚本是为命令行设计的,但没有在:)

核心概念是xcodebuild和lipo。

我在Xcode UI中尝试了许多配置,但都不起作用。因为这是一种批处理,所以命令行设计更适合,所以苹果逐渐从Xcode中删除了批构建功能。所以我不希望他们在未来提供基于UI的批量构建功能。

我把它做成了一个Xcode 4模板,与Karl的静态框架模板相同。

我发现构建静态框架(而不是普通的静态库)会导致LLVM随机崩溃,这是由于一个明显的链接器错误——所以,我猜静态库仍然有用!

IOS 10更新:

我在使用iphoneos10.0构建fatlib时遇到了一个问题,因为脚本中的正则表达式只期望9。X和更低,在ios 10.0中返回0.0

要解决这个问题,只需替换

SDK_VERSION=$(echo ${SDK_NAME} | grep -o '.\{3\}$')

SDK_VERSION=$(echo ${SDK_NAME} | grep -o '[\\.0-9]\{3,4\}$')

我需要一个脂肪静态库JsonKit,所以在Xcode中创建了一个静态库项目,然后在项目目录中运行这个bash脚本。只要你将xcode项目配置为“只构建活动配置”,你就应该在一个库中获得所有的架构。

#!/bin/bash
xcodebuild -sdk iphoneos
xcodebuild -sdk iphonesimulator
lipo -create -output libJsonKit.a build/Release-iphoneos/libJsonKit.a build/Release-iphonesimulator/libJsonKit.a