在启用位码的情况下编译 iOS 库

Compiling iOS library with bitcode enabled

我需要发布一个启用了位码的框架,结果很麻烦。我在项目的设置中将 'Enable Bitcode' 设置为 'YES' 并且它可以为真实设备和模拟器干净地构建。

我想测试这个库,所以我将它集成到我为此目的创建的新应用程序中,但现在它只为模拟器构建。当我尝试为真实设备构建时,我得到:

ld: '/path/to/Framework.framework/Company(File.o)' does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)

就像我说的,我是在启用 Bitcode 的情况下构建它的,所以我不确定为什么会这样。

有什么想法吗?谢谢

AFAIK,当您使用 Xcode 构建应用程序时,它仅在您制作存档时包含 Bitcode,原因 - 当只想调试或测试 app/library.[=19= 时减少编译时间]

为了确保 Xcode 在每次构建时发出位码,您可以将 -fembed-bitcode 标志添加到 Other C flagsOther linker flags:

此外,检查二进制文件是否包含位码的最简单方法是使用 otoolgrep:

otool -l binary_name | grep __LLVM

如果有位码,您将看到一个或多个 segname __LLVM 条目,如果没有,则输出为空。

使用 RunScript 代码的另一种方式:(构建设置 -- Build Phases RunScript)

# define output folder environment variable
UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal

# Step 1. Build Device and Simulator versions
xcodebuild -target TARGETNAME ONLY_ACTIVE_ARCH=NO -configuration Release -sdk iphoneos  BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}"
xcodebuild -target TARGETNAME -configuration Release -sdk iphonesimulator BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}"

# make sure the output directory exists
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"

# Step 2. Create universal binary file using lipo
lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/lib${PROJECT_NAME}.a" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/lib${PROJECT_NAME}.a" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/lib${PROJECT_NAME}.a"

# Last touch. copy the header files. Just for convenience
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/include" "${UNIVERSAL_OUTPUTFOLDER}/"

要使用 xcodebuild 命令启用 bitcode,您必须添加

BITCODE_GENERATION_MODE=bitcode.

找到下面的命令

xcodebuild BITCODE_GENERATION_MODE=bitcode -target TARGETNAME ONLY_ACTIVE_ARCH=NO -configuration Release -sdk iphoneos  BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}"