0

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

以下是项目当前状态的链接: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:找不到框架 Camiitool clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)

我试图解决这个问题的任何事情似乎都会产生相同错误的一些不同版本。例如“Module not found Camiitool”,或者我找到了 Camiitool,但随后我在方法调用 (nfc3d_version_fork) 上收到错误,表明该方法不存在。

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

运行 MacOS 10.15.7、Xcode 12.0

4

1 回答 1

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(".") 的原因。这次我决定采取不同的方法,排除所有不需要的东西,看看我完成后是否需要特别包含任何东西。原来我不需要包含任何资源,并且排除解决了我所有的错误和警告。

这是我更新的清单。

// 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"]),
    ]
)
于 2020-10-15T15:38:07.980 回答