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


当前回答

我需要一个脂肪静态库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

其他回答

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

我已经创建了一个XCode 4项目模板,它可以让你像创建常规库一样轻松地创建一个通用框架。

伟大的工作!我拼凑了一些类似的东西,但必须单独运行。让它成为构建过程的一部分会使构建过程简单得多。

有一点值得注意。我注意到它不会复制任何你标记为公共的包含文件。我把我剧本里的内容改编成了你的,效果相当不错。将以下内容粘贴到脚本的末尾。

if [ -d "${CURRENTCONFIG_DEVICE_DIR}/usr/local/include" ]
then
  mkdir -p "${CURRENTCONFIG_UNIVERSAL_DIR}/usr/local/include"
  cp "${CURRENTCONFIG_DEVICE_DIR}"/usr/local/include/* "${CURRENTCONFIG_UNIVERSAL_DIR}/usr/local/include"
fi

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

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

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

我试过了,很管用!