简体   繁体   English

Swift单元测试错误:找不到架构x86_64的符号(Swift软件包管理器)

[英]Swift Unit Test Error: symbol(s) not found for architecture x86_64 (Swift Package Manager)

I am having trouble getting unit tests to run in Swift projects created with the Swift Package Manager (that is, any unit tests created by the Package Manager... those I create from within Xcode work fine from within Xcode). 我无法让单元测试在使用Swift Package Manager创建的Swift项目中运行(也就是说,由Package Manager创建的任何单元测试...我在Xcode中创建的那些单元测试在Xcode中都可以正常工作)。 I am getting the same error on all projects generated from the Package Manager. 我从软件包管理器生成的所有项目上都遇到了相同的错误。 To keep it simple, I tried on a very basic test project with as little modification from the default setup as possible, but still getting the error. 为简单起见,我尝试了一个非常基本的测试项目,对默认设置进行了尽可能少的修改,但仍然出现错误。 Here are the steps to reproduce: 以下是重现步骤:

  1. Create a new project with swift package init --type executable (module name is Hello ) 使用swift package init --type executable创建一个新项目(模块名称为Hello
  2. Add the Xcode Project: swift package generate-xcodeproj 添加Xcode项目: swift package generate-xcodeproj
  3. In Xcode build settings, ensure Enable Testability is Yes 在Xcode构建设置中,确保“ 启用可测试性”为“是”
  4. In swift.main enter this simple test code: 在swift.main中输入以下简单测试代码:
import Foundation

let message = "Hello, world!"
print(message)
  1. In HelloTests.swift: 在HelloTests.swift中:
import XCTest
@testable import Hello

class HelloTests: XCTestCase {
    func testExample() {
        XCTAssert(message == "Hello, world!")
    }

    static var allTests = [
        ("testExample", testExample),
    ]
}
  1. Package.swift and XCTestManifests.swift left as-is. Package.swift和XCTestManifests.swift保持原样。
  2. It builds and runs fine with swift build and swift run Hello (Also, from within in Xcode). 它可以通过swift buildswift run Hello (也可以在Xcode中从内部) swift buildswift run Hello
  3. However, when running swift test or running any test in Xcode, the build fails with the following error message: 但是,在运行swift test或在Xcode中运行任何测试时,构建失败并显示以下错误消息:
Undefined symbols for architecture x86_64:
  "Hello.message.unsafeMutableAddressor : Swift.String", referenced from:
      implicit closure #1 : @autoclosure () throws -> Swift.Bool in HelloTests.HelloTests.testExample() -> () in HelloTests.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Somehow, it seems like it's failing to link the main module, so the symbols are not recognized. 不知何故,似乎无法链接主模块,因此无法识别符号。 However, I can't tell what's wrong or how to fix it. 但是,我不能说出什么问题或如何解决。

I downloaded one of the sample projects from GitHub , and generated the Xcode project. 从GitHub下载了一个示例项目 ,并生成了Xcode项目。 The tests for this project run perfectly in Xcode and the terminal. 该项目的测试可以在Xcode和终端中完美运行。 I've carefully compared the sample project to mine and can't tell what's different. 我已经仔细比较了示例项目和我的示例项目,无法分辨有什么不同。 Almost all setup code (Package.swift, file structure, etc.) and project setting are nearly identical. 几乎所有设置代码(Package.swift,文件结构等)和项目设置都几乎相同。 The only meaningful difference I can tell is that the sample project is a library/framework and mine is an executable (but seems like linking should work the same for both types). 我可以告诉的唯一有意义的区别是,示例项目是一个库/框架,而我的项目是一个可执行文件(但似乎链接对于两种类型都应该工作相同)。 Otherwise, I can't tell what they are doing right and I am doing wrong. 否则,我无法告诉他们他们在做什么,我做错了。

I figured it out (thanks to Cristik 's help). 我想通了(感谢Cristik的帮助)。 Executable modules are not testable (at least for now), so the solution was to move all definitions to a library module and leave just the main.swift file in the executable module. 可执行模块是不可测试的(至少到目前为止),因此解决方案是将所有定义移至库模块,仅将main.swift文件保留在可执行模块中。 That way, all unit tests were run with the library as a dependency vs. the executable. 这样,所有单元测试都以库作为依赖项与可执行文件的依赖关系运行。 The package.swift now looks like this: package.swift现在看起来像这样:

let package = Package(
    name: "HighestNumberPairing",
    products: [
        .executable(name: "HighestNumberPairing", targets: ["HighestNumberPairing"]),
        .library(name: "NumberPairing", targets: ["NumberPairing"]),
    ],
    dependencies: [],
    targets: [
        .target(
            name: "HighestNumberPairing",
            dependencies: ["NumberPairing"]),
        .target(
            name: "NumberPairing",
            dependencies: []),
        .testTarget(
            name: "NumberPairingTests",
            dependencies: ["NumberPairing"]),
    ]
)

The full program is here on Github . 完整的程序在Github上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift包管理器“从架构x86_64开始的未定义符号”来自命令行 - Swift Package Manager “Undefined symbols for architecture x86_64” from command line 找不到架构x86_64的符号-iOS - Symbol(s) not found for architecture x86_64 - iOS Swift Static Library based on C++ sources: linker command failed error: ld: symbol(s) not found for architecture x86_64 clang - Swift Static Library based on C++ sources: linker command failed error: ld: symbol(s) not found for architecture x86_64 clang 未找到架构x86_64的架构x86_64 &&符号的未定义符号 - Undefined symbols for architecture x86_64&&symbol(s) not found for architecture x86_64 Swift 项目中缺少所需的架构 x86_64 错误? - Missing required architecture x86_64 Error in Swift Project? 链接器错误:ld:找不到体系结构x86_64的符号 - Linker error: ld: symbol(s) not found for architecture x86_64 Xcode链接错误“找不到体系结构x86_64的符号” - Xcode link error “symbol(s) not found for architecture x86_64” Swift 测试给出错误“架构 x86_64 的未定义符号” - Swift test give error "Undefined symbols for architecture x86_64" 架构的未定义符号x86_64 iOS swift - Undefined symbols for architecture x86_64 iOS swift iOS Swift编程-体系结构x86_64的未定义符号 - iOS Swift Programming - Undefined symbols for architecture x86_64
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM