ios - 带有不安全构建标志 : target integrity error 的 Swift 包依赖项

标签 ios swift xcode dlib swiftpm

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链接器选项,类似于 headerSearchPathcxxSettings .

关于ios - 带有不安全构建标志 : target integrity error 的 Swift 包依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63064164/

相关文章:

ios - UITableView 更改一个单元格中的文本,其他单元格中的文本也更改

ios - Xcode 如何加载主 Storyboard ?

swift - 广泛使用的 Swift 扩展协议(protocol)

swift - 引用嵌套在导航和选项卡 Controller 中的 ViewController?

android - CSS Media Query Landscape - 键盘问题

ios - 如何以编程方式检查 ios 设备当前的 userInterfaceStyle?

ios - 当我编译 `GLSL` 代码时,出现错误 'ERROR: 0:15: ' premature EOF' : syntax error syntax error'

ios - 什么时候在 iOS AutoLayout 中使用 Multiplier?

ios - Xcode 8 Memory Graph 说 "No selection"并且不工作

iphone - NSArray 中的 SQL Sum 和 If/Case 语句