Swift 测试给出错误“架构 x86_64 的未定义符号"; [英] Swift test give error "Undefined symbols for architecture x86_64"

查看:25
本文介绍了Swift 测试给出错误“架构 x86_64 的未定义符号";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从命令行运行 swift test 来运行测试用例.这是测试用例:

I'm running swift test from the command line to run the test cases. This is the test case:

import XCTest
@testable import vnk_swift

class KeyMappingTests: XCTestCase {
    static var allTests : [(String, (KeyMappingTests) -> () throws -> Void)] {
        return [
            // ("testExample", testExample),
        ]
    }

    func testExample() {
        let keyMapping = KeyMapping()
        XCTAssertNotNil(keyMapping , "PASS")
    }
}

这是输出消息.

如果我删除 KeyMapping 的使用,一切正常:

If I remove the usage of KeyMapping, everything works fine:

    func testExample() {
        // let keyMapping = KeyMapping()
        XCTAssertNotNil(true , "PASS")
    }

看起来在我尝试使用类时出现问题.我该如何解决这个问题?

Looks like there is a problem when I'm trying to use a class. How do I fix this?

(我没有在这个项目中使用 Xcode,因为我开始使用 swift package init,这个项目的源代码在这里:https://github.com/trungdq88/vnk-swift)

(I did not use Xcode for this project as I started with swift package init, the source code for this project is here: https://github.com/trungdq88/vnk-swift)

推荐答案

通过进行以下修改,我成功地构建并测试了您的包:

I managed to successfully build and test your package by doing the following modifications:

  • 将包名称重命名为 VnkSwift,由于某些原因,构建工具不喜欢包名称中的破折号,当您生成的包名称中有下划线时它也不起作用(因此将包重命名为vnk_swift 以确保导入语句和包名匹配不起作用)
  • 将测试文件夹重命名为 VnkSwiftTests 以便链接器知道要链接的对象;似乎这是链接器知道链接到包的先决条件
  • 最后,将 main.swift 重命名为其他内容(我使用了 utils.swift).问题是 main.swift 的存在指示构建工具生成一个可执行文件,并且链接到一个可执行文件不能很好地工作.重命名后,必须注释if代码,因为全局运行代码只能属于main.swift.
  • renamed the package name to VnkSwift, for some reasons the build tool doesn't like dashes in package name, nor it works when you have underscores in the generated package name (so renaming the package to vnk_swift to make sure the import statement and the package name match didn't work)
  • renamed the test folder to VnkSwiftTests in order for the linker to know what to link against; seems this is a precondition for the linker to know to link against the package
  • finally, renamed main.swift to something else (I used utils.swift). The thing is that the presence of main.swift instructs the build tool to generate an executable, and linking against an executable doesn't work very well. After the rename, had to comment the if code, as global running code can only belong to main.swift.

总结:

  • 避免使用非字母数字的包名
  • 包名和测试目录名必须同步
  • 确保您没有 main.swift 文件以确保包可以链接到
  • Avoid non-alphanumeric package names
  • Package name and test directory name must be in sync
  • Make sure you don't have a main.swift file to make sure the package can be linked against

为什么不能将单元测试目标链接到可执行文件?

这是因为测试包和可执行包都有全局执行代码(也就是主函数),所以链接器不知道选择哪一个.从 Xcode 进行测试时,测试包会运行到应用程序的上下文中 - 它不会链接到它,就像这里的情况一样.

It's because both the test bundle and the executable bundle have global executing code (aka the main function), so the linker doesn't know which one of them to pick. When testing from Xcode, the test bundle runs into the context of the application - it doesn't link against it, like in the situation right here.

Xcode 在创建命令行工具时也有这个问题——你不能对那个目标进行单元测试,如果你想对代码进行单元测试,那么你必须创建一个由工具和单元链接的库测试目标(或包含两个目标中的文件)

Xcode also has this problem when creating a command line tool - you can't unit test that target, if you want to unit test the code then you have to create a library that will be linked by both the tool and the unit test target (or include the files in both targets)

这篇关于Swift 测试给出错误“架构 x86_64 的未定义符号";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆