从理论上讲,我们似乎可以构建一个包含模拟器、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)


当前回答

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\}$')

其他回答

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

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

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

我试过了,很管用!

实际上,我只是为了这个目的写了自己的脚本。它不使用Xcode。 (它基于Gambit Scheme项目中的一个类似脚本。)

基本上,它运行。/configure和make三次(适用于i386、armv7和armv7s),并将每个生成的库合并到一个胖库中。

有一个命令行实用程序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 12更新:

如果你运行xcodebuild而不带-arch参数,XCode 12将默认构建架构为“arm64 x86_64”的模拟器库。

然后运行xrun -sdk iphoneos lipo -create -output会有冲突,因为模拟器中有arm64架构,也有设备库。

我叉子脚本从亚当git和修复它。

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\}$')