构建单元测试目标时出现链接错误
我有一个 XCode4 / iOS 项目,带有常规目标和单元测试目标。一切工作正常,除了当我尝试 #import 我的测试类中的一个类并尝试使用它时。如果我尝试构建单元测试目标,则会收到以下链接错误:
Undefined symbols for architecture i386:
"_OBJC_CLASS_$_FRRCategory", referenced from:
objc-class-ref in CategoryTests.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
在 CategoryTests.m 中,我以这种方式导入头文件:
#import "../todoro/FRRCategory.h"
我做错了什么?
I have a XCode4 / iOS project with a regular target and unit test target. Everything works fine, except when I try to #import one of my classes in my test class and try to use it. If I try to build the unit test target, I get the following link error:
Undefined symbols for architecture i386:
"_OBJC_CLASS_$_FRRCategory", referenced from:
objc-class-ref in CategoryTests.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
In CategoryTests.m I'm importing the header file in this way:
#import "../todoro/FRRCategory.h"
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请按照此处的说明进行操作。它不需要您添加任何文件来编译源。
我首先错过了“默认隐藏的符号=否”应该适用于您的应用目标,而不是测试目标。
无论如何,它对我有用(tm)。
Follow the instructions here. It doesn't require you to add any files to compile sources.
I first missed that "Symbols Hidden by Default=NO" should be for your app target, not test target.
It worked for me anyway (tm).
确保 FRRCategory 源文件已添加到单元测试目标的编译源中。
Xcode 4:
项目导航器 -> “[项目名称]”->在 Targets 下选择您的单元测试目标 ->构建阶段 ->展开编译源 ->单击编译源底部的 + 并添加正确的源文件。
Make sure that the FRRCategory source file has been added to your Compile Sources for your unit test target.
Xcode 4:
Project Navigator -> "[Project Name]" -> Under Targets select your unit test target -> Build Phases -> Expand Compile Sources -> Click + at bottom of Compile sources and add the correct source file.
您可能遇到的另一个问题是您的单元测试是否使用实际应用程序中未使用的 C 函数(或类似函数)。
这可能仅限于有一个子项目。就我而言应用
我的单元测试使用了一些其他地方未使用的 C 函数,并且这些函数是从应用程序二进制文件中剥离的(而不是从子项目的.a 文件)。
修复方法是
(*不要对发布配置执行此操作,因为它会使用从未被调用的代码使应用程序膨胀)。
Another gotcha that you may hit is if your unit test is using C functions (or similar) that aren't used in the actual app.
This may be restricted to having a sub-project. In my case
My unit test used a few of the C functions that were not used anywhere else, and these were stripped from the app binary (NOT from the sub project's .a file).
The fix is to
(* don't do this to the release configs as it'll bloat the app with code that is never called).
如果您的文件位于框架内,则仅应引用导入内的文件夹。否则,将文件添加到项目后,只需执行
#import "FRRCategory.h"
即可。好吧,除非你对标题搜索路径做了一些奇怪的事情。You should only refer to a folder inside your import if your file is inside a framework. Otherwise, once you added your file to the project, simply do
#import "FRRCategory.h"
. Well, unless you did something weird with your Header Search Paths.就我而言,主机应用程序是“自定义”。我必须将其更改为 iOS 目标并设置“允许测试主机应用程序 API”。您可以在测试目标的常规选项卡上找到它。
In my case, the host app was "Custom". I had to change it to an iOS target and set "Allow testing Host Application APIs". You can find it on the General tab of your test target.