ios - 如何为 i386 和 x86_64 架构构建 Cocoa Touch Framework?

标签 ios objective-c cocoa-touch swift frameworks

构建 Cocoa Touch 框架(Swift 或 Object-C)并将其作为“嵌入式二进制文件”添加到另一个项目后,我在尝试构建时收到以下错误消息

missing required architecture i386
...
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

根据各种现有答案和扩展研究,我已经将 i386 添加到 Architectures build设置中......

enter image description here

但是这似乎没有效果。当我检查使用

lipo -info TesterFrameworkObjC

我只得到

Architectures in the fat file: TesterFrameworkObjC are: armv7 arm64

... i386(和 x86_64)不应该也出现在这里吗?我错过了什么?

(我正在使用 Xcode 6.2 + 为 iOS 8.2 构建)


一些新见解

使用这个 build script 的一个版本我能够为框架的 Swift 版本构建缺失的架构。

enter image description here

但是,当我将这个框架添加到我的应用程序并构建时,我仍然会遇到错误

Undefined symbols for architecture x86_64:
  "_OBJC_CLASS_$__TtC20TesterFrameworkSwift18TestFrameworkSwift", referenced from:
  objc-class-ref in ViewController.o
ld: symbol(s) not found for architecture x86_64

查看模拟器的最终构建文件夹构建

Build/Products/Debug-iphoneos/TesterFrameworkSwift.framework/

我可以看到架构仍然缺失,尽管它们是框架的一部分(如图所示)。如何确保在构建应用程序时包含我的框架的所有正确架构构建?


更新

查看给我的所有错误消息,看起来问题实际上并不是构建了错误的产品/架构,而是一步之后 Linker 没有使用正确的路径给他们。不确定这有多正确。

最佳答案

这是相同的problem我有。

这似乎是一个 Xcode 错误,我通过添加构建脚本来解决它。 它只会用所有架构编译你的库,并将它lipo 成胖二进制图像。

另请注意

Looking at the final build folder building for the simulator

Build/Products/Debug-iphoneos/TesterFrameworkSwift.framework/

您在 iphone 构建文件夹中查看 x86_64 和 i386 图像,它们应该在 Debug-iphonesimulator/TesterFrameworkSwift.framework/

您的脚本中还有一个递归问题,我已经更正了它。

UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal

if [ "true" == ${ALREADYINVOKED:-false} ]
then
echo "RECURSION: Detected, stopping"
else
export ALREADYINVOKED="true"

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

# Step 2. Copy the framework structure (from iphoneos build) to the universal folder
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework" "${UNIVERSAL_OUTPUTFOLDER}/"

# Step 3. Copy Swift modules (from iphonesimulator build) to the copied framework directory
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule/." "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule"

# Step 4. Create universal binary file using lipo and place the combined executable in the copied framework directory
lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework/${PROJECT_NAME}"

# Step 5. Convenience step to copy the framework to the project's directory
cp -R "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework" "${PROJECT_DIR}"

# Step 6. Convenience step to open the project's directory in Finder
open "${PROJECT_DIR}"

fi

6) Hit "cmd + B" (Build Project)

7) Open Product in Finder

enter image description here

8) Navigate 1 directory up ("cmd + ↑"), and you will see "Release-universal" directory. enter image description here

There will be your "fat/universal" library, You are ready to go!

You can check it via

file test.dylib 
test.dylib: Mach-O universal binary with 4 architectures
test.dylib (for architecture i386): Mach-O dynamically linked shared library i386
test.dylib (for architecture x86_64):   Mach-O 64-bit dynamically linked shared library x86_64
test.dylib (for architecture armv7):    Mach-O dynamically linked shared library arm
test.dylib (for architecture arm64):    Mach-O 64-bit dynamically linked shared library

关于ios - 如何为 i386 和 x86_64 架构构建 Cocoa Touch Framework?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28652650/

相关文章:

iOS、 swift : "Unable to simultaneously satisfy constraints" in syslog

ios - SFSafariViewController 在移动模式下不打开 url

objective-c - UISegmentedControl 分隔线图像未显示

ios - 为什么旋转 UIImageView 会影响其他 UI 元素?

cocoa-touch - 尝试将新项目添加到设置包中的 "Preference items"数组时,Xcode 4 崩溃

ios - GLKView 类的替代解决方案是什么? [在 iOS 12 中已弃用]

ios - 当它是一个单独的 Swift 文件时,从 TableViewDataSource 中删除或添加单元格

objective-c - 在 Objective-C 枚举中查找整数值

objective-c - NSOutlineView 设置最大列宽及其内容

ios - 在一个 UITableViewCell 上隐藏分隔线