错误:不支持将桥接头与模块接口一起使用命令CompileSwiftSources失败,退出代码为非零

问题描述

我已将其中一个静态库应用程序切换为Xcode12。我正在尝试进行XCFramework分发。运行构建命令后,

xcodebuild archive -scheme "MySDK" -sdk iphoneos  -archivePath “./archives/ios.xcarchive” -SKIP_INSTALL=NO

当我将Build Settings -> Build Libraries for distribution切换到YES时,我遇到了错误提示

<unkNown>:0: error: using bridging headers with module interfaces is unsupported
Command CompileSwiftSources Failed with a nonzero exit code

** ARCHIVE Failed **


The following build commands Failed:
    CompileSwiftSources normal armv7 com.apple.xcode.tools.swift.compiler
    CompileSwiftSources normal armv7s com.apple.xcode.tools.swift.compiler
    CompileSwiftSources normal arm64 com.apple.xcode.tools.swift.compiler
(3 failures)

This answer可以工作,但不幸的是创建.xcframework时需要设置选项分布YES

如何解决此问题?

解决方法

Apple 似乎正在弃用桥接标头以支持模块,但缺少关于如何包含 C 标头的their documentation。该信息可以在更详细的 clang docs 中找到。

尝试从项目设置中删除桥接头,而是创建一个 module.modulemap 文件:

module MySdk {
   header "MySdk-Bridging-Header.h"
   export *
}

这有一些警告,例如需要在您的 swift 代码中 import MySdk 并了解 C 函数将被公开,但这让我克服了困难。

我通过反复试验发现的一个技巧是将这个模块映射文件和 C 头文件包含在框架的 Headers 文件夹中。这允许任何使用该框架的应用自动检测 MySdk 作为模块。