如何在 Swift 包管理器中添加本地库(*.a 文件)作为依赖项?
我尝试在我的 Package.swift 中添加:
dependencies: [
// Dependencies declare other packages that this package depends on.,
.package(url: "file://../otherdirectory/x86_64-apple-macosx/debug/libTest.a")
],
但是当我运行'swift build'时出现这个错误
Package.swift:17:10: error: type of expression is ambiguous without more context
最佳答案
首先:package
依赖只能链接到其他包!
从 Swift 5.3 开始,使用 binaryTarget
是可能的,但您应该使用几个所需的架构(arm64、x86_64)构建静态库,然后使用下一个命令从它们创建 XCFramework:
xcodebuild -create-xcframework \
-library <path> [-headers <path>] \
[-library <path> [-headers <path>]...] \
-output <path>
例如:
xcodebuild -create-xcframework \
-library build/simulators/libMyStaticLib.a \
-library build/devices/libMyStaticLib.a \
-output build/MyStaticLib.xcframework
然后你可以在你的包中创建新的二进制目标依赖:
let package = Package(
name: "MyPackage",
...
targets: [
.target(
name: "MyPackage",
dependencies: ["MyStaticLib"]
),
.binaryTarget(name: "MyStaticLib", path: "path/MyStaticLib.xcframework"),
...
]
注意:xcframework的路径从项目的根目录开始(同Package.swift)。
关于swift - 如何在 Swift Package Manager 中添加本地库作为依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63607450/