我目前正在尝试为C库创建一个Swift包装器。为此,我使用Swift软件包管理器。
我的计划是首先创建一个system-module,它封装了想要的C-库。之后,这个库将包含在另一个Library类型的Swift包中。为了测试所有的东西,我想再做一个executable类型的Swift包
问题在最后一部分。可执行文件找不到对Library的引用。
为了更好地解释一切,下面是我的工作流程:
1)用:swift package init --type system-module创建了一个新的快速包。下面是最后一个Package.swift文件:
// swift-tools-version:4.2
import PackageDescription
let package = Package(
name: "Clibmongoc",
pkgConfig: "libmongoc-1.0",
providers: [
.brew(["mongo-c-driver"])
],
products: [
.library(name: "Clibmongoc", targets: ["Clibmongoc"])
]
)和.modulemap
module Clibmongoc [system] {
header "/usr/local/include/libmongoc-1.0/mongoc.h"
link "Clibmongoc"
export *
}2)现在我必须创建Library
首先,我用:swift package init --type Library初始化了它。由此产生的Package.swift是:
// swift-tools-version:4.2
import PackageDescription
let package = Package(
name: "SwiftLibMongoC",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "SwiftLibMongoC",
targets: ["SwiftLibMongoC"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "../Clibmongoc", from: "1.0.2")
],
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 which this package depends on.
.target(
name: "SwiftLibMongoC",
dependencies: ["Clibmongoc"]),
.testTarget(
name: "SwiftLibMongoCTests",
dependencies: ["SwiftLibMongoC"]),
]
)基本上,我只是把新创建的包复制到了新的库中。我还确保将system-module的git标记设置为1.0.2。我还确保将依赖项包含在目标中。正如预期的那样,在创建.xcodeproj时,我可以访问Clibmongoc system-module。
现在我们到了这个问题上。
3) --我用swift package init --type executable创建了测试模块,并将新创建的Library包含在清单中:
// swift-tools-version:4.2
import PackageDescription
let package = Package(
name: "SwiftLibMongoCTest",
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "../SwiftLibMongoC", from: "0.0.2")
],
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 which this package depends on.
.target(
name: "SwiftLibMongoCTest",
dependencies: ["SwiftLibMongoC"]),
.testTarget(
name: "SwiftLibMongoCTestTests",
dependencies: ["SwiftLibMongoCTest"]),
]
)同样,我确保git标记的版本被正确地设置为0.0.2,并且依赖项SwiftLibMongoC也被设置为目标中的依赖项。
当试图构建可执行文件时,它可以工作,但是当创建.xcodeproj时,我可以导入system-module Clibmongoc,但是我没有为新的Library SwiftLibMongoC获取auto completion。但是,如果导入并构建它,则不会出现错误,但是,如果我试图访问新Library中的默认生成的代码,则会得到一个错误:
未解析标识符的使用
任何援助都是非常感谢的。
发布于 2019-01-01 15:11:48
这其实是个愚蠢的错误。swift的默认访问级别是内部的,这意味着您无法从模块外部看到代码。我从来没有真正看过预生成的代码,因此我从未想过必须将默认的结构更改为public。
我希望将来这个问题能在文档中得到更好的解决。
https://stackoverflow.com/questions/53994956
复制相似问题