iOS构建链接Mac Catalyst对象文件错误的解决求助
我在Ionic/Angular应用中使用capacitor-background-fetch插件,Android平台运行正常,但iOS构建时出现以下链接错误:
ld: in /Users/sampath/Library/Developer/Xcode/DerivedData/App-eyizjewzpltdzgbapgcghtizmnbr/Build/Products/Debug-iphoneos/TSBackgroundFetch.framework/TSBackgroundFetch(TSBackgroundFetch_vers.o), building for iOS, but linking in object file built for Mac Catalyst, file '/Users/sampath/Library/Developer/Xcode/DerivedData/App-eyizjewzpltdzgbapgcghtizmnbr/Build/Products/Debug-iphoneos/TSBackgroundFetch.framework/TSBackgroundFetch' for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
环境信息
package.json依赖
{ "name": "se", "version": "1.0.0", "author": "se", "homepage": "se", "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "lint": "ng lint" }, "private": true, "dependencies": { "@angular/cdk": "^14.0.0", "@angular/common": "14.0.0", "@angular/core": "14.0.0", "@angular/forms": "14.0.0", "@angular/platform-browser": "14.0.0", "@angular/platform-browser-dynamic": "14.0.0", "@angular/router": "14.0.0", "@angular/youtube-player": "^14.0.0", "@awesome-cordova-plugins/core": "6.2.0", "@awesome-cordova-plugins/in-app-browser": "6.2.0", "@awesome-cordova-plugins/insomnia": "6.2.0", "@capacitor-community/barcode-scanner": "^3.0.1", "@capacitor-community/bluetooth-le": "2.0.1", "@capacitor/android": "4.3.0", "@capacitor/app": "4.0.1", "@capacitor/browser": "4.0.1", "@capacitor/camera": "^4.1.4", "@capacitor/core": "^4.3.0", "@capacitor/device": "4.1.0", "@capacitor/geolocation": "4.1.0", "@capacitor/haptics": "4.0.1", "@capacitor/ios": "4.4.0", "@capacitor/keyboard": "4.0.1", "@capacitor/preferences": "4.0.1", "@capacitor/splash-screen": "4.1.0", "@capacitor/status-bar": "4.0.1", "@ionic/angular": "6.1.9", "@transistorsoft/capacitor-background-fetch": "^1.0.3", "@types/lodash": "4.14.185", "cordova-plugin-inappbrowser": "5.0.0", "cordova-plugin-insomnia": "4.3.0", "dayjs": "1.11.5", "ionicons": "6.0.3", "lodash": "4.17.21", "rxjs": "6.6.0", "tslib": "2.2.0", "zone.js": "0.11.4" }, "devDependencies": { "@angular-devkit/build-angular": "14.2.10", "@angular-eslint/builder": "13.0.1", "@angular-eslint/eslint-plugin": "13.0.1", "@angular-eslint/eslint-plugin-template": "13.0.1", "@angular-eslint/template-parser": "13.0.1", "@angular/cli": "14.0.0", "@angular/compiler": "14.0.0", "@angular/compiler-cli": "14.0.0", "@angular/language-service": "14.0.0", "@capacitor/cli": "^4.3.0", "@ionic/angular-toolkit": "6.0.0", "@types/node": "12.11.1", "@typescript-eslint/eslint-plugin": "5.3.0", "@typescript-eslint/parser": "5.3.0", "eslint": "7.6.0", "eslint-plugin-import": "2.22.1", "eslint-plugin-jsdoc": "30.7.6", "eslint-plugin-prefer-arrow": "1.2.2", "ts-node": "8.3.0", "typescript": "4.7.3" }, "description": "se" }
ionic info
Ionic: Ionic CLI : 6.19.0 (/usr/local/lib/node_modules/@ionic/cli) Ionic Framework : @ionic/angular 6.1.9 @angular-devkit/build-angular : 14.2.10 @angular-devkit/schematics : 14.0.0 @angular/cli : 14.0.0 @ionic/angular-toolkit : 6.0.0
Capacitor信息
Capacitor CLI : 4.7.1 @capacitor/android : 4.3.0 @capacitor/core : 4.7.1 @capacitor/ios : 4.4.0
工具版本
cordova-res (update available: 0.15.4) : 0.15.3 native-run : 1.7.2
系统信息
NodeJS : v16.14.2 (/usr/local/bin/node) npm : 8.5.0 OS : macOS
其他版本
XCode: 14.2 Mac OS: 13.2.1
重要提示:我之前在Ionic/Angular应用中使用过数百个原生插件都没问题,只有这个插件出现该问题,求解决方案。
解决方案
方案1:禁用Mac Catalyst架构
在Xcode中打开iOS项目,按以下步骤操作:
- 选中项目根节点,进入Build Settings
- 搜索
Excluded Architectures,在Debug和Release配置中,为iOS添加arm64到排除架构列表 - 搜索
Build Active Architecture Only,将Debug模式设置为YES
方案2:更新插件到兼容版本
当前使用的插件版本为1.0.3,更新到适配Capacitor 4和Xcode 14的最新版本:
npm install @transistorsoft/capacitor-background-fetch@latest
更新后重新同步Capacitor:
npx cap sync ios
方案3:清理缓存与重建iOS平台
- 关闭Xcode,删除DerivedData缓存:
rm -rf ~/Library/Developer/Xcode/DerivedData
- 回到项目目录,清理并重建iOS平台:
npx cap remove ios npx cap add ios npx cap sync ios
方案4:手动移除插件的Mac Catalyst架构
使用lipo命令检查插件framework的支持架构:
lipo -info ios/App/Pods/TSBackgroundFetch/TSBackgroundFetch.framework/TSBackgroundFetch
若包含Mac Catalyst架构,手动移除:
lipo -remove arm64-apple-ios-macabi ios/App/Pods/TSBackgroundFetch/TSBackgroundFetch.framework/TSBackgroundFetch -output ios/App/Pods/TSBackgroundFetch/TSBackgroundFetch.framework/TSBackgroundFetch
内容的提问来源于stack exchange,提问作者Sampath

