问题描述
如何在Swift Package Manager中添加本地库(* .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
依赖项只能链接到其他软件包!
可以使用binaryTarget
从 Swift 5.3 开始,但是您应该使用几种所需的体系结构(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相同)。