【问题标题】:How to output a static library from a Swift package?如何从 Swift 包输出静态库?
【发布时间】:2022-12-18 21:10:35
【问题描述】:

我有一个 swift 包,我想将它集成到我的 CI 中并从中创建一个静态库。

这是 Package.Swift 的样子:

import PackageDescription

let package = Package(
    name: "CMyLibrary",
    platforms: [
        .iOS(.v11),
        .watchOS(.v6),
    ],
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "CMyLibrary",
            targets: ["CMyLibrary"]),
    ],
    dependencies: [],
    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: "CMyLibrary",
            dependencies: ["CWolfCrypt", "CWolfSsl"],
            path: "Sources/CMyLibrary",
            cSettings: [
                .headerSearchPath("./"),
            ]),
        .target(
            name: "CWolfCrypt",
            dependencies: [],
            path: "Sources/CWolfCrypt",
            cSettings: [
                .headerSearchPath("./"),
            ]),
        .target(
            name: "CWolfSsl",
            dependencies: ["CWolfCrypt"],
            path: "Sources/CWolfSsl"),
    ],
    cLanguageStandard: .c11
)

执行 swift build 命令会创建一个包含许多文件但不是静态库的构建文件夹。

我是否可以假设我应该能够从这个 swift 包中输出一个静态库?如果是,我该怎么做才能真正获得静态库(也许是一个 .a 文件?)

【问题讨论】:

    标签: ios swift continuous-integration static-libraries swift-package-manager


    【解决方案1】:

    好的,我通过将 type: .static, 添加到产品中的库说明符来修复它。

    【讨论】:

      【解决方案2】:

      是的,你应该能够从你的 Swift 包中创建一个静态库。为此,您需要使用带有 -c release 和 -Xlinker -L/path/to/library 标志的 swift build 命令。这将在发布模式下构建您的包并将其链接到指定的库。

      下面是 swift build 命令如何使用这些标志的示例:

      swift build -c release -Xlinker -L/path/to/library
      

      运行此命令后,您应该会在包的 .build/release 目录中看到一个静态库文件(带有 .a 文件扩展名)。这是您可以在 CI 中使用的静态库。

      请记住,您需要使用 -Xlinker -L/path/to/library 标志指定包所依赖的静态库的路径。这是必要的,因为 swift build 命令不会自动将您的包链接到任何依赖项。

      我希望这有帮助!如果您有任何其他问题,请告诉我。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-09
        • 2021-07-29
        • 1970-01-01
        • 2015-06-22
        • 1970-01-01
        • 1970-01-01
        • 2021-02-26
        • 1970-01-01
        相关资源
        最近更新 更多