Building for iOS Simulator, but linking in dylib built for iOS. for architecture arm64

iOS混编项目从x86 macbook迁移到arm架构的Macbook,项目编译失败的解决方案

本文写于2021年8月,完善与2021年11月,xcode版本号为13,请注意时效。本文的方法属于盲人摸象式的不完全归纳法,是“bug + bug = 成功运行”的典型范例;

背景

2014年MacbookPro换2020年MacBook Air (M1芯片)
新设备Xcode版本为12,操作系统为11.5.1 , Big Sur
项目中使用cocoapod引入网易云信 NIMSDK_Lite最新的版本;

故障现象

项目在老设备上使用模拟器可以正常编译,拷贝到新设备后,模拟器编译报错:
大致的错误内容为:

building for iOS Simulator, but linking in dylib built for iOS

或者

Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture arm64

但是如果是真机编译则没有问题;

问题分析

新的设备是Apple M1芯片,xcode12使用模拟器编译时,只需要一套设备上用的arm64的架构来编译即可,但是有些Pod库会不支持这样的用法,Xcode12 + M1芯片设备的组合不知道为啥不能智能处理,也许是兼容的bug吧,总之具体原因不详。

解决方案

我按照以下方案解决了上述问题;
https://stackoverflow.com/questions/63607158/xcode-12-building-for-ios-simulator-but-linking-in-object-file-built-for-ios

划重点:

Basically you have to exclude arm64 for simulator architecture both from your project and the Pod project,

  • To do that, navigate to Build Settings of your project and add Any iOS Simulator SDKwith value arm64 inside Excluded Architecture.
XGVJM.png

注意,重要的事情说三遍:

both from your project and the Pod project

both from your project and the Pod project

both from your project and the Pod project


重要说明:

上面的方面可以解决在模拟器上运行APP的问题,但是真机联调和Archive 打包还是有问题的。
会报错:

Unable to find a profile type for platform iOS and purpose development. You may need to reinstall Xcode.

如果你有幸能遇到这个问题,请不要真的傻乎乎的去重新安装Xcode了。
请试试修改Xcode的编译器为老的模式;

截屏2021-11-28 上午11.33.17.png

Xcode菜单>File>Workspace Settings

截屏2021-11-28 上午11.33.30.png

修改Build System为:Legacy Build System(Deprecated)


番外篇:

上面的方案的确管用了一阵,但是后来又不管用了。我在项目中使用cocoaPod导入了NIMSDK_Lite,当我删除项目中引入的类库 SPaySDK后,项目就无法正常编译了。提示都是 与 NIMSDK_lite 类库相关的一些错误。
上面的方法全都失效了。
之前能用,可能是因为 bug + bug = 意外修复bug的情况吧。

错误提示为:
Building for iOS Simulator, but linking in dylib built for iOS, file '/Users/xxxx/iOS/kdh_iOS/Pods/NIMSDK_LITE/NIMSDK/NIMSDK.framework/NIMSDK' for architecture arm64

最后,使用下面的方案彻底解决问题:
https://stackoverflow.com/questions/65978359/xcode-error-building-for-ios-simulator-but-linking-in-dylib-built-for-ios-f

  • Quit Xcode.
  • Go to Finder and open Application Folder.
  • Right click on Xcode application, select Get Info
  • In the "Xcode Info Window" check on Open using Rosetta.
  • Open Xcode and try running again.
1031635861546_.pic_hd.jpg

总结 - 成功状态截图

我这边最后的成功状态如下:

  • 模拟器可以正常运行;
  • 真机联调可以正常运行;
  • Archive 打包可以正常运行;
  • 在切换运行方式时,不需要调整任何配置。

设备型号

截屏2021-11-28 上午11.43.53.png

Xcode 13.1

截屏2021-11-28 上午11.38.50.png

Build System为:Legacy Build System(Deprecated)

截屏2021-11-28 上午11.33.30.png

Xcode 的打开模式为: Rosetta

1031635861546_.pic_hd.jpg

Excluded Architectures 配置:(APP target)

截屏2021-11-28 上午11.39.45.png

Excluded Architectures 配置:(Pods target)

截屏2021-11-28 上午11.41.42.png

Enable Bitcode = NO

包括:

  • App Target > Build Settings > Enable Bitcode
  • Pods Project > Build Settings > Enable Bitcode


    截屏2021-11-28 上午11.47.14.png
截屏2021-11-28 上午11.47.30.png

模拟器运行时,build Configuration 要选成 Debug

截屏2021-12-04 下午7.27.04.png

你可能感兴趣的:(Building for iOS Simulator, but linking in dylib built for iOS. for architecture arm64)