我目前正在尝试为C库创建Swift Wrapper。为此,我正在使用Swift软件包管理器。
我的计划是首先创建一个system-module
,用于包装所需的C库。之后,该库将包含在另一个Library
类型的Swift包中。为了测试所有内容,我想制作另一个executable
问题出在最后一部分。可执行文件找不到对Library
的引用。
为更好地解释一切,这是我的工作流程:
1)
使用swift package init --type system-module
创建一个新的swift包。
这是最终的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
,但不能获得auto completion
新的Library
SwiftLibMongoC
。但是,如果我导入并构建它,则不会出错,但是,如果尝试从新Library
的{{1}}访问默认生成的代码,则会收到错误消息:
使用未解析的标识符
非常感谢任何帮助。
答案 0 :(得分:0)
这实际上是一个愚蠢的错误。 swift的默认访问级别是内部访问,这意味着您无法从模块外部查看代码。我从来没有真正看过预先生成的代码,因此也从未想过必须将默认结构更改为public。
我希望将来可以在文档中更好地解决此问题。