[英]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:
以下是重现步骤:
swift package init --type executable
(module name is Hello
) swift package init --type executable
创建一个新项目(模块名称为Hello
) swift package generate-xcodeproj
swift package generate-xcodeproj
import Foundation
let message = "Hello, world!"
print(message)
import XCTest
@testable import Hello
class HelloTests: XCTestCase {
func testExample() {
XCTAssert(message == "Hello, world!")
}
static var allTests = [
("testExample", testExample),
]
}
swift build
and swift run Hello
(Also, from within in Xcode). swift build
和swift run Hello
(也可以在Xcode中从内部) swift build
并swift run Hello
。 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.