Swift 包管理器动态库 [英] Swift Package Manager dynamic library

查看:44
本文介绍了Swift 包管理器动态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've recently converted a bunch of my frameworks to use Swift Package Manager. My Package.swift looks something like this:

// 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) 链接到我的应用程序中.根据包文档中的库产品定义,如果我愿意,我可以将我的库的 type 指定为 .dynamic:

Since the library used to be a framework, I'd like to link it in my app as a dynamic library (.dylib). According to the library product definition in the Package documentation, I can specify the type of my library to be .dynamic if I want:

库的可选类型,用于确定如何链接到库.不指定此参数以让 Swift 包管理器在静态或动态链接之间进行选择(推荐).如果您不支持这两种链接类型,请为此参数使用 .static 或 .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 默认构建一个静态库,这不是我想要的.

If I leave it as nil, Xcode defaults to building a static library when I link this package in my app project, which is not what I want.

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

If I set the type to .dynamic in my library's Package.swift, Xcode builds a .dylib, but it doesn't get embedded in the application, leading to a linker error:

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 添加到复制文件构建阶段的简单方法...

I also see no easy way of adding the .dylib to a Copy Files Build Phase...

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

So what's the recommended way of asking SPM to build and link a dynamic library through Xcode? Is this something that is not yet supported?

推荐答案

我想通了!

如果你想创建一个框架库,你需要像这样强制它在 Package.swift 中是一个:

If you want to create a framework library, you need to force it to be one in the Package.swift like so:

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

完成此操作后,您将在链接到库的项目的 Xcode 项目设置中看到一个嵌入选项.选择目标时,向下滚动到 Frameworks、Libraries 和 Embedded Content 部分.您应该会看到嵌入动态库依赖项的选项:

Once you've done that, you'll see an embed option in the Xcode project settings of the project linking to the library. When selecting the target, scroll down to the Frameworks, Libraries, and Embedded Content section. You should see an option to embed your dynamic library dependencies:

这篇关于Swift 包管理器动态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆