【问题标题】:Exporting public C headers with SwiftPM使用 SwiftPM 导出公共 C 标头
【发布时间】:2021-05-04 06:05:13
【问题描述】:

我有一个开源的 Objective C 框架,我也想使用 Swift 包管理器来提供它。

通常,我在 Xcode 项目中设置了公共标头,在构建框架时,这些标头被复制到框架包下,并在被 Xcode 链接时发现。

我不能,但是让它与 SwiftPM 一起工作。

我为我的框架创建了一个模块映射:

framework module LNPopupController {
    umbrella header "LNPopupController.h"
    export *
    module * { export * }
}

我在 Package.swift 中这样定义库:

let package = Package(
    name: "LNPopupController",
    platforms: [
        .iOS(.v12),
        .macOS(.v10_15)
    ],
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "LNPopupController",
            type: .dynamic,
            targets: ["LNPopupController"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .target(
            name: "LNPopupController",
            dependencies: [],
            path: "LNPopupController",
            publicHeadersPath: ".",
            cSettings: [
                .headerSearchPath("."),
                .headerSearchPath("Private"),
            ]),
    ]
)

在 Xcode 中作为项目依赖添加时,框架编译得很好,但是当依赖目标尝试 import LNPopupController 时,会抛出错误:umbrella header 'LNPopupController.h' not found

查看构建文件夹,确实,我看到 Xcode 已经构建了一个二进制文件,但没有复制公共头文件。

有什么方法可以指定哪些标头是公开的,并让构建系统复制它们以供导入?

【问题讨论】:

  • 你有没有让这个工作?把我的头发拉出来......
  • @scaly 我做到了,是的。我会用我所做的来回答我自己的问题。

标签: swift-package-manager


【解决方案1】:

我终于明白了。这就是我为LNPopupController所做的:

我创建了一个include/LNPopupController/ 子目录树,并在其中放置了指向所有公共标头的软链接。

在包文件中,我添加了publicHeadersPath: "include"。

您可以在此处查看最终结果: https://github.com/LeoNatan/LNPopupController/blob/master/Package.swift

这可能不是最理想的方法,但它适用于我尝试过此方法的所有 ObjC 项目。

【讨论】:

    猜你喜欢
    • 2015-02-11
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    • 2011-02-14
    • 2014-07-02
    • 1970-01-01
    • 2016-04-29
    相关资源
    最近更新 更多