You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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,仍会触发编译错误

解决方案

  1. 锁定GoogleMobileAds稳定版本
    避免使用main分支(可能包含未稳定代码),切换到官方推荐的稳定版本。修改Package.swift中的依赖声明:

    .package(url: "https://github.com/googleads/swift-package-manager-google-mobile-ads.git", from: "11.0.0"),
    

    版本号可根据官方最新稳定版调整。

  2. 显式声明传递依赖的链接设置
    Swift Package默认不会自动传递底层依赖的链接规则,需在自定义包的target中添加linkerSettings:

    .target(
        name: "MikrasyaLibraryAds",
        dependencies: [
            .product(name: "GoogleMobileAds", package: "swift-package-manager-google-mobile-ads")
        ],
        linkerSettings: [
            .linkedFramework("MarketplaceKit"),
            .linkedFramework("CoreAudioTypes")
        ]
    ),
    
  3. 清理缓存后重新构建

    • 执行File > Packages > Reset Package Caches
    • 执行Product > Clean Build Folder
    • 重新编译项目
  4. 检查主项目编译配置

    • 确保主项目的iOS Deployment Target不低于自定义Package指定的iOS 15
    • 确认Build Options中的Enable Linking with Swift Libraries已勾选

内容的提问来源于stack exchange,提问作者Mikrasya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 18:24:51