TL; 博士
尝试在 Xcode 的 iOS 项目中使用本地 Swift 包依赖项时,我收到以下错误:The package product 'DlibWrapper' cannot be used as a dependency of this target because it uses unsafe build flags.
(我使用不安全标志来指定静态库位置)
我还尝试将该包作为基于远程分支的依赖项导入,但无论如何它都失败了。
根据 this post在 Swift 论坛上,该问题已在不久前解决,相应的 pull request已经合并了。
问题出现在 Swift 5.2.4 (Xcode 11.6) 和 5.3 (Xcode 12 beta 3) 中。
任何线索可能是什么问题?
细节
我正在尝试构建一个包装 dlib 的 Swift 包库并在 iOS 应用程序中使用它。由于平台原因,我无法使用 .systemLibrary
目标链接 dlib
.所以我在一个静态库中预编译它并与包装器代码打包在一起,如下所示:
DlibWrapper/
Libraries/
dlib/
include/
...
lib/
arm64/
libdlib.a
Sources/
CWrapper/
include/
module.modulemap
cwrapper.h
cwrapper.cpp
SwiftWrapper/
SwiftWrapper.swift
Package.swift
DlibWrapper/Package.swift
的简化内容:// swift-tools-version:5.3
import PackageDescription
let package = Package(
name: "DlibWrapper",
platforms: [
.iOS(.v13)
],
products: [
.library(
name: "DlibWrapper",
targets: ["CWrapper", "SwiftWrapper"])
],
dependencies: [],
targets: [
.target(
name: "SwiftWrapper",
dependencies: ["CWrapper"]
),
.target(
name: "CWrapper",
cxxSettings: [.headerSearchPath("../../Libraries/dlib/include")],
linkerSettings: [
.linkedLibrary("dlib"),
.linkedFramework("Accelerate", .when(platforms: [.iOS])),
// The error is caused by this line
.unsafeFlags(["-LLibraries/dlib/lib/arm64"], .when(platforms: [.iOS])),
]
),
],
cxxLanguageStandard: .cxx1z
)
我尝试使用 link
module.modulemap
内的属性(property)但编译器似乎忽略了它。另外,在 .linkedLibrary()
中给出库的绝对路径在目标 list 中没有帮助,链接器提示它找不到库。任何解决方法的想法? (作为最后的手段,我可能会将所有内容打包在一个框架中)
将不胜感激任何帮助。
谢谢
最佳答案
更新
事实证明下面的方法实际上不起作用。 Swift 完全忽略 .linkedLibrary
当包被编译为静态库时(检查 swift build --verbose
)。该设置仅用于动态库。尽管如此,:
语法在后一种情况下没有任何作用 - 无论如何都找不到库。
(以下答案因历史原因而保留,也许可以帮助某人找到解决方案)
旧答案
解决方法是在链接器选项中使用 clang/gcc 冒号语法,即 -l:
(减去-el-冒号)。见 this answer更多细节。
这是包 list 中的相关片段:
// swift-tools-version:5.3
import PackageDescription
let package = Package(
name: "DlibWrapper",
//...
targets: [
//...
.target(
name: "CWrapper",
cxxSettings: [.headerSearchPath("../../Libraries/dlib/include")],
linkerSettings: [
// Note the `:` in front of the relative path
.linkedLibrary(":Libraries/dlib/lib/arm64/libdlib.a", .when(platforms: [.iOS])),
//...
]
),
],
//...
)
解释.linkedLibrary
链接器设置必不可少的提供了 -l
链接器的标志。所以,无论你放在那里作为参数都将被翻译成这样的swift build -Xlinker -l<your-linkedLibrary-string>
:
语法允许使用非规范名称指定库,即实际文件名。最后,因为链接器的工作目录设置为包根目录,我们可以为它提供库的相对路径。我仍然认为这是一种解决方法,因为正确的方法是使用类似
librarySearchPath
的方法。 ,即 -L
链接器选项,类似于 headerSearchPath
在 cxxSettings
.
关于ios - 带有不安全构建标志 : target integrity error 的 Swift 包依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63064164/