Swift Package依赖GoogleMobileAds致项目编译链接错误求助
Swift Package依赖GoogleMobileAds后项目编译报错(Undefined symbols/Linker失败)
问题现象
- 自定义Swift Package(
MikrasyaLibraryAds)依赖GoogleMobileAds,自身编译无报错 - 通过GitHub将该包添加到主项目后,
Package Dependencies栏可正常看到GoogleMobileAds及其依赖GoogleUserMessagingPlatform - 主项目编译失败,抛出
Undefined symbols及Linker command failed with exit code 1错误,且target的「Frameworks, Libraries, and Embedded Content」中找不到GoogleMobileAds
自定义Package.swift代码
let package = Package( name: "MikrasyaLibraryAds", platforms: [.iOS(.v15)], products: [ .library( name: "MikrasyaLibraryAds", targets: ["MikrasyaLibraryAds"]), ], dependencies: [ .package(url: "https://github.com/googleads/swift-package-manager-google-mobile-ads.git", branch: "main"), ], targets: [ .target( name: "MikrasyaLibraryAds", dependencies: [ .product(name: "GoogleMobileAds", package: "swift-package-manager-google-mobile-ads"), ] ), ] )
编译日志
ld: warning: Could not find or use auto-linked framework 'CoreAudioTypes': framework 'CoreAudioTypes' not found ld: warning: Could not find or use auto-linked framework 'MarketplaceKit': framework 'MarketplaceKit' not found ld: Undefined symbols: enum case for MarketplaceKit.AppDistributor.testFlight(MarketplaceKit.AppDistributor.Type) -> MarketplaceKit.AppDistributor, referenced from: l007 in GoogleMobileAds[arm64][399](GADMarketplaceKitSignals.o) l008 in GoogleMobileAds[arm64][399](GADMarketplaceKitSignals.o) enum case for MarketplaceKit.AppDistributor.marketplace(MarketplaceKit.AppDistributor.Type) -> (Swift.String) -> MarketplaceKit.AppDistributor, referenced from: l007 in GoogleMobileAds[arm64][399](GADMarketplaceKitSignals.o) l008 in GoogleMobileAds[arm64][399](GADMarketplaceKitSignals.o) enum case for MarketplaceKit.AppDistributor.other(MarketplaceKit.AppDistributor.Type) -> MarketplaceKit.AppDistributor, referenced from: l007 in GoogleMobileAds[arm64][399](GADMarketplaceKitSignals.o) l008 in GoogleMobileAds[arm64][399](GADMarketplaceKitSignals.o) static MarketplaceKit.AppDistributor.current.getter : MarketplaceKit.AppDistributor, referenced from: function signature specialization <Arg[0] = Dead> of static GoogleMobileAds.GADMarketplaceKitSignals.appDistributor() async -> Swift.String in GoogleMobileAds[arm64][399](GADMarketplaceKitSignals.o) async function pointer to static MarketplaceKit.AppDistributor.current.getter : MarketplaceKit.AppDistributor, referenced from: function signature specialization <Arg[0] = Dead> of static GoogleMobileAds.GADMarketplaceKitSignals.appDistributor() async -> Swift.String in GoogleMobileAds[arm64][399](GADMarketplaceKitSignals.o) enum case for MarketplaceKit.AppDistributor.appStore(MarketplaceKit.AppDistributor.Type) -> MarketplaceKit.AppDistributor, referenced from: l007 in GoogleMobileAds[arm64][399](GADMarketplaceKitSignals.o) l008 in GoogleMobileAds[arm64][399](GADMarketplaceKitSignals.o) type metadata accessor for MarketplaceKit.AppDistributor, referenced from: l007 in GoogleMobileAds[arm64][399](GADMarketplaceKitSignals.o) l008 in GoogleMobileAds[arm64][399](GADMarketplaceKitSignals.o) nominal type descriptor for MarketplaceKit.AppDistributor, referenced from: _symbolic _____Sg 14MarketplaceKit14AppDistributorO in GoogleMobileAds[arm64][399](GADMarketplaceKitSignals.o) __swift_FORCE_LOAD_$_swiftXPC, referenced from: __swift_FORCE_LOAD_$_swiftXPC_$_GoogleMobileAds in GoogleMobileAds[arm64][399](GADMarketplaceKitSignals.o) clang: error: linker command failed with exit code 1 (use -v to see invocation)
额外观察
- 移除自定义包,直接给主项目添加GoogleMobileAds,编译正常
- 同时保留自定义包和直接添加的GoogleMobileAds,仍会触发编译错误
解决方案
锁定GoogleMobileAds稳定版本
避免使用main分支(可能包含未稳定代码),切换到官方推荐的稳定版本。修改Package.swift中的依赖声明:.package(url: "https://github.com/googleads/swift-package-manager-google-mobile-ads.git", from: "11.0.0"),版本号可根据官方最新稳定版调整。
显式声明传递依赖的链接设置
Swift Package默认不会自动传递底层依赖的链接规则,需在自定义包的target中添加linkerSettings:.target( name: "MikrasyaLibraryAds", dependencies: [ .product(name: "GoogleMobileAds", package: "swift-package-manager-google-mobile-ads") ], linkerSettings: [ .linkedFramework("MarketplaceKit"), .linkedFramework("CoreAudioTypes") ] ),清理缓存后重新构建
- 执行
File > Packages > Reset Package Caches - 执行
Product > Clean Build Folder - 重新编译项目
- 执行
检查主项目编译配置
- 确保主项目的
iOS Deployment Target不低于自定义Package指定的iOS 15 - 确认
Build Options中的Enable Linking with Swift Libraries已勾选
- 确保主项目的
内容的提问来源于stack exchange,提问作者Mikrasya
相关产品推荐
相关产品推荐

