Xcode unittest 构建失败,错误为“架构 x86_64 的未定义符号”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30935640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode unittest build failed with error "Undefined symbols for architecture x86_64"
提问by Yuwen Yan
My unittest target build failed with below error:
我的 unittest 目标构建失败,错误如下:
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_MCStore", referenced from:
objc-class-ref in MCStoreTests.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Basic information:
基本信息:
- Xcode6.2
- iOS8.2 SDK
- Xcode6.2
- iOS8.2 SDK
What I have checked:
我检查过的:
Symbols Hidden by Default
is NoOther Linker Flags
is -framework XCTestFramework Search Paths
is $(SDKROOT)/Developer/Library/Frameworks $(inherited)
Symbols Hidden by Default
没有Other Linker Flags
is -framework XCTestFramework Search Paths
是 $(SDKROOT)/Developer/Library/Frameworks $(继承的)
回答by Yuwen Yan
回答by Ben Thielker
At least as of Xcode 7.3 test targets let you select a "Host Application". In the Test target (but not presently the UI Test target) this automatically populates the "Test Host" build setting, but not the "Bundle Loader", which can lead to classes not being found.
至少从 Xcode 7.3 测试目标开始,您可以选择“主机应用程序”。在 Test 目标(但目前不是 UI Test 目标)中,这会自动填充“Test Host”构建设置,而不是“Bundle Loader”,这会导致找不到类。
Considering this, if you set your test targets' "Bundle Loader" Build Setting to $(TEST_HOST)
, it will always contain the right value even if you change the Host Application.
考虑到这一点,如果您将测试目标的“Bundle Loader”构建设置设置为$(TEST_HOST)
,即使您更改主机应用程序,它也将始终包含正确的值。
This is effectively the oppositeof the advice given in the link @yuwen-yan posted, and should amount to less work.
这实际上与@yuwen-yan 发布的链接中给出的建议相反,应该减少工作量。
回答by Peter Lamberg
This error may be the result of having wrong test target type, namely ui test target.
这个错误可能是由于测试目标类型错误,即ui test target。
UI test targets can't use the internals of the main target, not even with @testable
imports. Unit test targets OTOH can use the internals.
UI 测试目标不能使用 main target 的内部结构,甚至不能使用@testable
导入。单元测试目标 OTOH 可以使用内部结构。
See more details in this answer.
(I believe this has changed in some XCode version which causes confusion. Typical way is just to include a huge bunch of files from the original target in the ui test target. A proper way is to design UI tests in such a way that they don't need or use much code from the main target.)
(我相信这在某些 XCode 版本中发生了变化,这会导致混淆。典型的方法是在 ui 测试目标中包含来自原始目标的大量文件。正确的方法是设计 UI 测试,使它们不不需要或使用来自主要目标的大量代码。)
回答by Raz
Experienced the same issue.
What fixed it for me was setting enable modules (c and objective-c)
to YESin the Testing target Build Settings
.
遇到过同样的问题。对我来说修复它的是在 Testing target 中设置enable modules (c and objective-c)
为YESBuild Settings
。
回答by Fyodor Volchyok
In my particular case I was trying to test release
configuration and was getting this particular error. After experimenting with different compilation flags I found out that setting Enable Testability
for release configuration on my projects (not particular targets but might work too) did the trick.
在我的特殊情况下,我试图测试release
配置并收到此特定错误。在尝试了不同的编译标志后,我发现Enable Testability
在我的项目(不是特定目标,但也可能工作)上设置发布配置可以解决问题。
回答by user1105951
In my case,I got this error when I need to test class that exist in frameworkthat was part of my app :
就我而言,当我需要测试作为我的应用程序一部分的框架中存在的类时,我收到了此错误:
testing framework class.
1. Add testing target to macOS .
2. Select project/targets window. Select the test target.
3. Go to “build phases”, “link binary with libraries”, add the frameworkyou want to test .
4. In the test class (setup/teardown), add “import” to the class you want to test from that framework
测试框架类。
1. 将测试目标添加到 macOS 。
2. 选择项目/目标窗口。选择测试目标。
3. 进入“构建阶段”,“将二进制文件与库链接”,添加要测试的框架。
4.在测试类(setup/teardown)中,添加“import”到你想从那个框架测试的类
回答by Bruno Muniz
What worked for me was just using the test target on the podfile.
对我有用的只是在 podfile 上使用测试目标。
target 'MyAppName' do
use_frameworks!
pod 'SwiftLint'
pod 'RxSwift'
pod 'RxCocoa'
pod 'RxDataSources'
pod 'RxGRDB'
target 'UnitTestTarget' do
inherit! :search_paths
end
end
回答by Deo Kalule
So here is what worked for me...
所以这对我有用......
override func setUp() {
super.setUp()
let promise = expectation(description: "App has finished running")
DispatchQueue.global(qos: .background).async{
// Wait on the background thread
sleep(4)
DispatchQueue.main.async {
// Fullfill the promise in the main thread
promise.fulfill()
}
}
// Initialize the storyboard
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// Get the view controller
sut = storyboard.instantiateViewController(withIdentifier: String(describing: ViewController.self)) as? ViewController
_ = sut.view
waitForExpectations(timeout: 5) { (_) in
// Finish set up after the app is done running its code
}
}// End setUp() Method
回答by Alexander Stepanishin
Experienced the same linker error after adding test target to old project which was created 2 or 3 Xcode versions ago. Furthermore, project has various xcodeproject/target/bundle names. All possible renames, cleanings, Build settings
, Build phases
, Scheme
manipulations did not worked for me.
将测试目标添加到 2 或 3 个 Xcode 版本之前创建的旧项目后,遇到相同的链接器错误。此外,项目有各种 xcodeproject/target/bundle 名称。所有可能的重命名、清理、Build settings
、Build phases
、Scheme
操作对我都不起作用。
What did actually worked after a long struggle is recreating the project along with all targets from scratch in latest Xcode version. It finally links! And in this case you don't even need to manually modify Search paths
, Bundle loader
, Xcode will do it for you.
经过长时间的努力,真正起作用的是在最新的 Xcode 版本中从头开始重新创建项目以及所有目标。终于连上了!在这种情况下,您甚至不需要手动修改Search paths
, Bundle loader
,Xcode 会为您完成。
回答by Nish
Below are the steps that I performed to fix the issue while trying to add unit test target in Xcode 9:
以下是我在尝试在 Xcode 9 中添加单元测试目标时为解决该问题而执行的步骤:
- Go to 'Manage Schemes'.
- Click on '+' button at the bottom.
- Select the newly added Target and select 'OK'.
- Make sure 'Shared' option is selected for the newly added target.
- 转到“管理方案”。
- 单击底部的“+”按钮。
- 选择新添加的目标并选择“确定”。
- 确保为新添加的目标选择了“共享”选项。