gpt4 book ai didi

Swift 包管理器动态库

转载 作者:行者123 更新时间:2023-12-01 17:48:19 25 4
gpt4 key购买 nike

我最近将我的一些框架转换为使用 Swift Package Manager。我的 Package.swift 看起来像这样:

// swift-tools-version:5.1
import PackageDescription

let package = Package(
name: "MDFToolbox",
platforms: [
.macOS(.v10_13), .iOS(.v12), .tvOS(.v12), .watchOS(.v3)
],
products: [
.library(name: "MDFToolbox", targets: ["MDFToolbox"])
],
dependencies: [
.package(url: "git@github.com:Swinject/Swinject.git", from: "2.7.0"),
],
targets: [
.target(name: "MDFToolbox", dependencies: ["Swinject"]),
]
)

由于该库曾经是一个框架,因此我想将其作为动态库 (.dylib) 链接到我的应用程序中。根据 Package 文档中的库产品定义,如果我愿意,我可以将库的 type 指定为 .dynamic:

The optional type of the library that is used to determine how to link to the library. Leave this parameter unspecified to let to let the Swift Package Manager choose between static or dynamic linking (recommended). If you do not support both linkage types, use .static or .dynamic for this parameter.

如果我将其保留为nil,当我在应用程序项目中链接此包时,Xcode 默认会构建静态库,这不是我想要的。

如果我在库的 Package.swift 中将类型设置为 .dynamic,Xcode 会构建一个 .dylib,但它不会嵌入到应用程序中,导致链接器错误:

dyld: Library not loaded: @rpath/libMDFToolbox.dylib
Referenced from: /Users/mpdifran/Library/Developer/Xcode/DerivedData/Remind-eewbkbjpfrqbdwchjrbmrtxzsjew/Build/Products/Debug-maccatalyst/Remind.app/Contents/MacOS/Remind
Reason: no suitable image found. Did find:
/Users/mpdifran/Library/Developer/Xcode/DerivedData/Remind-eewbkbjpfrqbdwchjrbmrtxzsjew/Build/Products/Debug-maccatalyst/libMDFToolbox.dylib: code signature in (/Users/mpdifran/Library/Developer/Xcode/DerivedData/Remind-eewbkbjpfrqbdwchjrbmrtxzsjew/Build/Products/Debug-maccatalyst/libMDFToolbox.dylib) not valid for use in process using Library Validation: mapped file has no cdhash, completely unsigned? Code has to be at least ad-hoc signed.

我还发现没有简单的方法将 .dylib 添加到复制文件构建阶段...

那么要求 SPM 通过 Xcode 构建和链接动态库的推荐方法是什么?这是尚不支持的东西吗?

最佳答案

我明白了!

如果你想创建一个框架库,你需要强制它成为 Package.swift 中的一个,如下所示:

.library(name: "MDFToolbox", type: .dynamic, targets: ["MDFToolbox"])

完成此操作后,您将在链接到库的项目的 Xcode 项目设置中看到一个嵌入选项。选择目标时,向下滚动到“框架、库和嵌入式内容”部分。您应该会看到一个嵌入动态库依赖项的选项:

enter image description here

关于Swift 包管理器动态库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58090099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com