找不到 C 库的 SwiftPM 框架

SwiftPM Framework not found for C library

我正在使用 Swift 包管理器为现有的 C 库创建一个 Swift 包装器。我认为我已经正确构建了所有内容,但是在创建和构建 Xcode 项目时出现错误。我已经尝试了所有我能找到的东西,但没有运气。正式让我难过。

这是项目当前状态的 link:https://github.com/JKcompute/Camiitool/tree/stackpost

这是我的 Package.swift

// swift-tools-version:5.3
import PackageDescription

let package = Package(
    name: "CamiitoolPackage",
    products: [
        .library(name: "Camiitool", targets: ["Camiitool"]),
        .library(name: "amiitoolSwift", targets: ["amiitoolSwift"]),
    ],
    dependencies: [
    ],
    targets: [
        .target(
            name: "amiitoolSwift",
            dependencies: [ 
               "Camiitool"
            ],
            path: "./Sources/amiitoolSwift"
        ),

        .target(
            name: "Camiitool",
            dependencies: [ 
                "mbedtls"
            ],
            path: "./Sources/Camiitool",
            exclude: [
                "mbedtls",
                "LICENSE"
            ],
            resources: [
                .copy("."),
            ]
        ),
        .target(
            name: "mbedtls",
            dependencies: [],
            path: "./Sources/Camiitool/mbedtls",
            exclude: [
                "./mbedtls/yotta",
                "./mbedtls/visualc",
                "LICENSE"
            ],
            resources: [
                .copy("."),
            ]
        ),  

        .testTarget(
            name: "amiitoolSwiftTests",
            dependencies: ["amiitoolSwift"]
        )
    ]
)

所以我这里的 Camiitool 是 C 库 Amiitool。 Amiitool 依赖于 mbedtls。所以我有一个 mbedtls 的目标,Camiitool,然后我也有一个 amiitoolSwift,我计划让我的 swift API.

这是我的来自 amiitoolSwift 目标的 swift 文件。注意:目前还很基础,仅供测试。

import Foundation
import Camiitool

public class Amii {
    var someString = "test"

    public init() {
        someString = "initialized"
    }
    
    public func printTest() {
        print(someString)
    }

     public func methodJustForTesting() {
        print(someString)

        let cString: UnsafePointer<CChar> = Camiitool.nfc3d_version_fork()

       print(String(cString: cString))
     }
}

问题是,当我构建时,我收到 Camiitool 的“未找到框架”错误。

> Ld /Users/ME/Library/Developer/Xcode/DerivedData/CamiitoolPackage-fjhqfruljmetilfuiykpzwhzobkc/Build/Products/Debug/amiitoolSwift.framework/Versions/A/amiitoolSwift normal (in target 'amiitoolSwift' from project 'CamiitoolPackage')
cd /Users/ME/GIT/PERSONAL/Amiibo/backup-wip/Camiitool
/Applications/Xcode_12.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -target x86_64-apple-macos10.10 -dynamiclib -isysroot /Applications/Xcode_12.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -L/Users/ME/Library/Developer/Xcode/DerivedData/CamiitoolPackage-fjhqfruljmetilfuiykpzwhzobkc/Build/Products/Debug -F/Users/ME/Library/Developer/Xcode/DerivedData/CamiitoolPackage-fjhqfruljmetilfuiykpzwhzobkc/Build/Products/Debug -F/Applications/Xcode_12.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -filelist /Users/ME/Library/Developer/Xcode/DerivedData/CamiitoolPackage-fjhqfruljmetilfuiykpzwhzobkc/Build/Intermediates.noindex/CamiitoolPackage.build/Debug/amiitoolSwift.build/Objects-normal/x86_64/amiitoolSwift.LinkFileList -install_name @rpath/amiitoolSwift.framework/Versions/A/amiitoolSwift -Xlinker -rpath -Xlinker /usr/lib/swift -Xlinker -rpath -Xlinker /Applications/Xcode_12.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -Xlinker -object_path_lto -Xlinker /Users/ME/Library/Developer/Xcode/DerivedData/CamiitoolPackage-fjhqfruljmetilfuiykpzwhzobkc/Build/Intermediates.noindex/CamiitoolPackage.build/Debug/amiitoolSwift.build/Objects-normal/x86_64/amiitoolSwift_lto.o -Xlinker -export_dynamic -Xlinker -no_deduplicate -fobjc-link-runtime -L/Applications/Xcode_12.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -L/usr/lib/swift -Xlinker -add_ast_path -Xlinker /Users/ME/Library/Developer/Xcode/DerivedData/CamiitoolPackage-fjhqfruljmetilfuiykpzwhzobkc/Build/Intermediates.noindex/CamiitoolPackage.build/Debug/amiitoolSwift.build/Objects-normal/x86_64/amiitoolSwift.swiftmodule -framework Camiitool -framework mbedtls -Xlinker -dependency_info -Xlinker /Users/ME/Library/Developer/Xcode/DerivedData/CamiitoolPackage-fjhqfruljmetilfuiykpzwhzobkc/Build/Intermediates.noindex/CamiitoolPackage.build/Debug/amiitoolSwift.build/Objects-normal/x86_64/amiitoolSwift_dependency_info.dat -o /Users/ME/Library/Developer/Xcode/DerivedData/CamiitoolPackage-fjhqfruljmetilfuiykpzwhzobkc/Build/Products/Debug/amiitoolSwift.framework/Versions/A/amiitoolSwift

ld: framework not found Camiitool clang: error: linker command failed with exit code 1 (use -v to see invocation)

我尝试解决的任何问题似乎都会产生相同错误的不同版本。例如“找不到模块 Camiitool”,或者我已经找到了 Camiitool,但是我在方法调用时遇到错误 (nfc3d_version_fork),该方法不存在。

最后一点:我项目的当前状态是基于我发现的这个例子:https://github.com/RoaringBitmap/SwiftRoaring 除了没有子模块外,这与我正在做的事情基本相同。我已经拉了这个项目,我能够构建并且 运行 它很好。我已经比较了项目之间的所有设置,我没有注意到任何差异。这让我最终 post 来到这里。

运行MacOS 10.15.7,Xcode12.0

所以我想出了这个。

首先我注意到,即使正在生成 .framework 文件,它也大部分是空的。这显然看起来不对,所以我开始研究一个单独的项目以确保我没有遗漏任何步骤。我能够让另一个项目工作,所以我回到这个项目并做了一些更改。

看来这是我的 5.3 版设置问题。我回到 5.1 并收到重复符号的错误。一旦我通过排除不需要的 c 文件来清除它们。我能够让它在 5.1

上运行

为了回到 5.1,我需要删除原始清单中的 resources.copy 个参数。所以当我回到 5.3 时,我收到了一堆关于未处理资源的警告:

warning: found 424 file(s) which are unhandled; explicitly declare them as resources or exclude from the target

我之前收到过这个警告,这就是我首先添加 resource.copy(".") 的原因。这次我决定采取不同的方法,排除所有不需要的东西,看看我是否需要在完成后特别包含任何东西。原来我不需要包含任何资源,exclude 解决了我所有的错误和警告。

这是我更新的清单。

// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "CamiitoolPackage",
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(name: "Camiitool", targets: ["Camiitool"]),
        .library(name: "amiitoolSwift", targets: ["amiitoolSwift"]),
        .library(name: "amiitoolSwiftDynamic", type: .dynamic,  targets: ["amiitoolSwift"]),
    ],
    dependencies: [
    ],
    targets: [
        .target(
            name: "amiitoolSwift",
            dependencies: [ 
               "Camiitool"
            ],
            path: "./Sources/amiitoolSwift"
        ),
        .target(
            name: "Camiitool",
            dependencies: [ 
                "mbedtls"
            ],
            path: "./Sources/Camiitool",
            exclude: [
                "./mbedtls",
                "./mbedtls/yotta",
                "./mbedtls/visualc",
                "./LICENSE",
                "./amiitool.c",
                "./README.md",
                "./Makefile"
            ]
        ),
        .target(
            name: "mbedtls",
            dependencies: [],
            path: "./Sources/Camiitool/mbedtls",
            exclude: [
                "./yotta",
                "./visualc",
                "./LICENSE",
                "./programs",
                "./tests",
                "./scripts",
                "./include/CMakeLists.txt",
                "./DartConfiguration.tcl",
                "./doxygen",
                "./library/Makefile",
                "./apache-2.0.txt",
                "./Makefile",
                "./README.md",
                "./ChangeLog",
                "./CMakeLists.txt",
                "./circle.yml",
                "./configs/README.txt",
                "./library/CMakeLists.txt"
            ]
        ),  
        .testTarget(
            name: "amiitoolSwiftTests",
            dependencies: ["amiitoolSwift"]
        )
    ]
)

现在它可以构建,不会生成任何警告或错误,并且我的 .framework 文件已按预期完成。我认为这里可能有一个错误,我没有得到更好的错误,因为 xcode 会显示我的 Camiitool.framework 构建成功。尽管显然不是。

然后我能够使用以下可执行程序包设置测试我的程序包,这也能正常工作。

// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "packageTester",

    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(name: "CamiitoolPackage", path: "../Camiitool"),
    ],
    targets: [
        .target(
            name: "packageTester",
            dependencies: [
                .product(name: "amiitoolSwift", package: "CamiitoolPackage")
            ]
        ),
        .testTarget(
            name: "packageTesterTests",
            dependencies: ["packageTester"]),
    ]
)