看来我们理论上可以构建一个包含模拟器,iPhone和iPad的静态库。
但是,Apple没有关于此的文档,我找不到,并且Xcode的默认模板未配置为执行此操作。
我正在寻找一种简单,可移植,可重复使用的技术,可以在Xcode中完成。
一些历史:
参考文献:
这是一个好主意,这是一个很好的方法,但它不起作用:http://www.drobnik.com/touch/2010/04/universal-static-libraries/
另一位SO提问者询问如何在不使用xcode的情况下执行此操作,以及专注于arm6与arm7部分的响应 - 但忽略了i386部分:How do i compile a static library (fat) for armv6, armv7 and i386
答案 0 :(得分:268)
备选方案:
Easy copy/paste of latest version(但安装说明可能会更改 - 请参阅下文!)
Karl's library需要花费更多精力来设置,但更好的长期解决方案(它将您的库转换为框架)。
Use this, then tweak it to add support for Archive builds - c.f. @Frederik在下面评论他正在使用的更改,以便在存档模式下很好地工作。
最近的变化: 1.增加了对iOS 10.x的支持(同时保持对旧平台的支持)
有关如何将此脚本与项目嵌入到另一个项目中使用的信息(尽管我强烈建议不要这样做,但是 - 如果嵌入项目,Apple在Xcode中有几个显示阻止错误相互之间,从Xcode 3.x到Xcode 4.6.x)
Bonus脚本让你自动包含Bundles(即包括来自你库的PNG文件,PLIST文件等) - 见下文(滚动到底部)
现在支持iPhone5(使用Apple的解决方案来处理lipo中的错误)。注意:安装说明已更改(我可以通过以后更改脚本来简化此操作,但现在不想冒风险)
“copy headers”部分现在尊重公共标题位置的构建设置(由Frederik Wallner提供)
添加了SYMROOT的显式设置(也可能需要设置OBJROOT?),感谢Doug Dickinson
SCRIPT(这是你必须复制/粘贴的)
有关使用/安装说明,请参阅下文
##########################################
#
# c.f. https://stackoverflow.com/questions/3520977/build-fat-static-library-device-simulator-using-xcode-and-sdk-4
#
# Version 2.82
#
# Latest Change:
# - MORE tweaks to get the iOS 10+ and 9- working
# - Support iOS 10+
# - Corrected typo for iOS 1-10+ (thanks @stuikomma)
#
# Purpose:
# Automatically create a Universal static library for iPhone + iPad + iPhone Simulator from within XCode
#
# Author: Adam Martin - http://twitter.com/redglassesapps
# Based on: original script from Eonil (main changes: Eonil's script WILL NOT WORK in Xcode GUI - it WILL CRASH YOUR COMPUTER)
#
set -e
set -o pipefail
#################[ Tests: helps workaround any future bugs in Xcode ]########
#
DEBUG_THIS_SCRIPT="false"
if [ $DEBUG_THIS_SCRIPT = "true" ]
then
echo "########### TESTS #############"
echo "Use the following variables when debugging this script; note that they may change on recursions"
echo "BUILD_DIR = $BUILD_DIR"
echo "BUILD_ROOT = $BUILD_ROOT"
echo "CONFIGURATION_BUILD_DIR = $CONFIGURATION_BUILD_DIR"
echo "BUILT_PRODUCTS_DIR = $BUILT_PRODUCTS_DIR"
echo "CONFIGURATION_TEMP_DIR = $CONFIGURATION_TEMP_DIR"
echo "TARGET_BUILD_DIR = $TARGET_BUILD_DIR"
fi
#####################[ part 1 ]##################
# First, work out the BASESDK version number (NB: Apple ought to report this, but they hide it)
# (incidental: searching for substrings in sh is a nightmare! Sob)
SDK_VERSION=$(echo ${SDK_NAME} | grep -o '\d\{1,2\}\.\d\{1,2\}$')
# Next, work out if we're in SIM or DEVICE
if [ ${PLATFORM_NAME} = "iphonesimulator" ]
then
OTHER_SDK_TO_BUILD=iphoneos${SDK_VERSION}
else
OTHER_SDK_TO_BUILD=iphonesimulator${SDK_VERSION}
fi
echo "XCode has selected SDK: ${PLATFORM_NAME} with version: ${SDK_VERSION} (although back-targetting: ${IPHONEOS_DEPLOYMENT_TARGET})"
echo "...therefore, OTHER_SDK_TO_BUILD = ${OTHER_SDK_TO_BUILD}"
#
#####################[ end of part 1 ]##################
#####################[ part 2 ]##################
#
# IF this is the original invocation, invoke WHATEVER other builds are required
#
# Xcode is already building ONE target...
#
# ...but this is a LIBRARY, so Apple is wrong to set it to build just one.
# ...we need to build ALL targets
# ...we MUST NOT re-build the target that is ALREADY being built: Xcode WILL CRASH YOUR COMPUTER if you try this (infinite recursion!)
#
#
# So: build ONLY the missing platforms/configurations.
if [ "true" == ${ALREADYINVOKED:-false} ]
then
echo "RECURSION: I am NOT the root invocation, so I'm NOT going to recurse"
else
# CRITICAL:
# Prevent infinite recursion (Xcode sucks)
export ALREADYINVOKED="true"
echo "RECURSION: I am the root ... recursing all missing build targets NOW..."
echo "RECURSION: ...about to invoke: xcodebuild -configuration \"${CONFIGURATION}\" -project \"${PROJECT_NAME}.xcodeproj\" -target \"${TARGET_NAME}\" -sdk \"${OTHER_SDK_TO_BUILD}\" ${ACTION} RUN_CLANG_STATIC_ANALYZER=NO" BUILD_DIR=\"${BUILD_DIR}\" BUILD_ROOT=\"${BUILD_ROOT}\" SYMROOT=\"${SYMROOT}\"
xcodebuild -configuration "${CONFIGURATION}" -project "${PROJECT_NAME}.xcodeproj" -target "${TARGET_NAME}" -sdk "${OTHER_SDK_TO_BUILD}" ${ACTION} RUN_CLANG_STATIC_ANALYZER=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" SYMROOT="${SYMROOT}"
ACTION="build"
#Merge all platform binaries as a fat binary for each configurations.
# Calculate where the (multiple) built files are coming from:
CURRENTCONFIG_DEVICE_DIR=${SYMROOT}/${CONFIGURATION}-iphoneos
CURRENTCONFIG_SIMULATOR_DIR=${SYMROOT}/${CONFIGURATION}-iphonesimulator
echo "Taking device build from: ${CURRENTCONFIG_DEVICE_DIR}"
echo "Taking simulator build from: ${CURRENTCONFIG_SIMULATOR_DIR}"
CREATING_UNIVERSAL_DIR=${SYMROOT}/${CONFIGURATION}-universal
echo "...I will output a universal build to: ${CREATING_UNIVERSAL_DIR}"
# ... remove the products of previous runs of this script
# NB: this directory is ONLY created by this script - it should be safe to delete!
rm -rf "${CREATING_UNIVERSAL_DIR}"
mkdir "${CREATING_UNIVERSAL_DIR}"
#
echo "lipo: for current configuration (${CONFIGURATION}) creating output file: ${CREATING_UNIVERSAL_DIR}/${EXECUTABLE_NAME}"
xcrun -sdk iphoneos lipo -create -output "${CREATING_UNIVERSAL_DIR}/${EXECUTABLE_NAME}" "${CURRENTCONFIG_DEVICE_DIR}/${EXECUTABLE_NAME}" "${CURRENTCONFIG_SIMULATOR_DIR}/${EXECUTABLE_NAME}"
#########
#
# Added: StackOverflow suggestion to also copy "include" files
# (untested, but should work OK)
#
echo "Fetching headers from ${PUBLIC_HEADERS_FOLDER_PATH}"
echo " (if you embed your library project in another project, you will need to add"
echo " a "User Search Headers" build setting of: (NB INCLUDE THE DOUBLE QUOTES BELOW!)"
echo ' "$(TARGET_BUILD_DIR)/usr/local/include/"'
if [ -d "${CURRENTCONFIG_DEVICE_DIR}${PUBLIC_HEADERS_FOLDER_PATH}" ]
then
mkdir -p "${CREATING_UNIVERSAL_DIR}${PUBLIC_HEADERS_FOLDER_PATH}"
# * needs to be outside the double quotes?
cp -r "${CURRENTCONFIG_DEVICE_DIR}${PUBLIC_HEADERS_FOLDER_PATH}"* "${CREATING_UNIVERSAL_DIR}${PUBLIC_HEADERS_FOLDER_PATH}"
fi
fi
安装说明
...奖金可选用法:
如果找不到输出文件,可以采用以下解决方法:
将以下代码添加到脚本的最末端(由Frederik Wallner提供):打开“$ {CREATING_UNIVERSAL_DIR}”
Apple删除200行后的所有输出。选择目标,在运行脚本阶段,您必须取消选中:“在构建日志中显示环境变量”
如果你正在为XCode4使用自定义的“构建输出”目录,那么XCode会将你所有的“意外”文件放在错误的位置。
...这是您的Universal Build的位置。
如何在项目中包含“非源代码”文件(PNG,PLIST,XML等)
将构建的软件包自动复制到与FAT静态库相同的文件夹中的脚本:
echo "RunScript2:"
echo "Autocopying any bundles into the 'universal' output folder created by RunScript1"
CREATING_UNIVERSAL_DIR=${SYMROOT}/${CONFIGURATION}-universal
cp -r "${BUILT_PRODUCTS_DIR}/"*.bundle "${CREATING_UNIVERSAL_DIR}"
答案 1 :(得分:80)
我花了很多时间尝试构建一个可以在armv7,armv7s和模拟器上运行的胖静态库。最后found a solution。
要点是分别构建两个库(一个用于设备,一个用于模拟器),重命名它们以区分彼此,然后将它们创建为一个库。
lipo -create libPhone.a libSimulator.a -output libUniversal.a
我尝试了它并且有效!
答案 2 :(得分:74)
我制作了一个XCode 4 project template,可以像制作常规库一样轻松制作通用框架。
答案 3 :(得分:30)
有一个命令行实用程序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中尝试了很多配置,但没有任何效果。由于这是一种批处理,因此命令行设计更为合适,因此Apple逐渐从Xcode中删除了批量构建功能。所以我不希望他们将来提供基于UI的批量构建功能。
答案 4 :(得分:9)
我需要一个用于JsonKit的胖静态库,因此在Xcode中创建了一个静态lib项目,然后在项目目录中运行了这个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
答案 5 :(得分:7)
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\}$')
答案 6 :(得分:4)
我把它变成了Xcode 4 template,与Karl的静态框架模板一样。
我发现由于明显的链接器错误,构建静态框架(而不是普通的静态库)会导致LLVM随机崩溃 - 所以,我想静态库仍然有用!
答案 7 :(得分:2)
干得好!我把类似的东西砍在一起,但不得不单独运行它。让它只是构建过程的一部分使它变得更加简单。
一项注意事项。我注意到它不会复制您标记为公共的任何包含文件。我已经将我脚本中的内容改编为你的内容并且运行得相当好。将以下内容粘贴到脚本的末尾。
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
答案 8 :(得分:2)
XCode 12更新:
如果在没有xcodebuild
参数的情况下运行-arch
,则XCode 12将默认使用体系结构“ arm64 x86_64”构建模拟器库。
然后运行xcrun -sdk iphoneos lipo -create -output
会发生冲突,因为arm64
体系结构存在于模拟器以及设备库中。
我fork script from Adam git并将其修复。
答案 9 :(得分:1)
为此我实际上只是wrote my own script。它不使用Xcode。 (它基于Gambit Scheme项目中的类似脚本。)
基本上,它运行./configure并制作三次(对于i386,armv7和armv7s),并将每个生成的库组合成一个胖lib。