M1 MacBook下CocoaPod无法同时集成LibTorch-Lite与TensorFlowLiteSwift
解决TensorFlowLiteSwift与LibTorch-Lite CocoaPod同时集成的冲突问题
问题背景
我在为自研CocoaPod同时添加以下两个依赖时遇到冲突:单独安装任意一个依赖均可正常工作,但无法同时集成:
s.dependency 'TensorFlowLiteSwift' s.dependency 'LibTorch-Lite'
LibTorch-Lite需要配置特定的spec参数才能避免架构编译错误:
s.static_framework = true s.pod_target_xcconfig = { 'HEADER_SEARCH_PATHS' => '$(inherited) ${PODS_ROOT}/LibTorch-Lite/install/include', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' } s.user_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' }
不配置上述参数会触发以下错误:
libtorch.a(empty.cpp.o), building for iOS Simulator, but linking in object file built for iOS, file '/var/folders/3x/x73_p70n2tg0gnwq9lct7rmr0000gn/T/CocoaPods-Lint-20221219-7300-1w8h64c-PyTorchTensorFlowCocoaPod/Pods/LibTorch-Lite/install/lib/libtorch.a' for architecture arm64
但配置EXCLUDED_ARCHS[sdk=iphonesimulator*]为arm64后,集成TensorFlowLiteSwift时又会出现模块找不到的错误:
main.swift:1:8: error: could not find module 'PyTorchTensorFlowCocoaPod' for target 'arm64-apple-ios-simulator'; found: x86_64-apple-ios-simulator, at: /Users/steve.ham/Library/Developer/Xcode/DerivedData/App-dnxlikhybfsytahdpnmquhblzyly/Build/Products/Release-iphonesimulator/PyTorchTensorFlowCocoaPod/PyTorchTensorFlowCocoaPod.framework/Modules/PyTorchTensorFlowCocoaPod.swiftmodule
解决方案
1. 仅为LibTorch-Lite单独配置架构排除规则
不要在全局的user_target_xcconfig中设置EXCLUDED_ARCHS,仅将该规则限定在LibTorch-Lite的Pod目标范围内,避免影响TensorFlowLiteSwift和主Pod的架构编译。修改后的spec配置如下:
s.static_framework = true # 仅为LibTorch-Lite的Pod目标配置架构排除,不影响全局用户目标 s.pod_target_xcconfig = { 'HEADER_SEARCH_PATHS' => '$(inherited) ${PODS_ROOT}/LibTorch-Lite/install/include', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' } # 移除user_target_xcconfig中的EXCLUDED_ARCHS配置
2. 为主Pod添加多架构编译支持
确保主Pod的spec明确支持模拟器的arm64和x86_64架构,让Xcode生成通用架构的产物:
# 在主Pod的spec中添加以下配置 s.pod_target_xcconfig = { 'VALID_ARCHS[sdk=iphonesimulator*]' => 'arm64 x86_64', 'ARCHS[sdk=iphonesimulator*]' => 'arm64 x86_64' }
3. 清理缓存并重新安装依赖
执行以下命令清除CocoaPods和Xcode的编译缓存,避免旧缓存导致的冲突:
# 清理所有Pod缓存 pod cache clean --all # 删除Pod锁定文件和Pods目录 rm -rf Podfile.lock Pods # 重新安装依赖 pod install # 清理Xcode派生数据 rm -rf ~/Library/Developer/Xcode/DerivedData
4. 升级TensorFlowLiteSwift版本
确认使用的TensorFlowLiteSwift版本支持arm64模拟器架构,较新版本已修复相关兼容性问题,建议升级到最新稳定版。
内容的提问来源于stack exchange,提问作者Steve Ham
相关产品推荐
相关产品推荐

