构建单元测试目标时出现链接错误

发布于 2024-11-03 01:13:04 字数 461 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

南烟 2024-11-10 01:13:04

请按照此处的说明进行操作。它不需要您添加任何文件来编译源。

我首先错过了“默认隐藏的符号=否”应该适用于您的应用目标,而不是测试目标。

无论如何,它对我有用(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).

把回忆走一遍 2024-11-10 01:13:04

确保 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.

十秒萌定你 2024-11-10 01:13:04

您可能遇到的另一个问题是您的单元测试是否使用实际应用程序中未使用的 C 函数(或类似函数)。

这可能仅限于有一个子项目。就我而言应用

  • 程序
    • 子项目
      • 嵌入AC库(即直接在内部编译.c和.h文件)

我的单元测试使用了一些其他地方未使用的 C 函数,并且这些函数是从应用程序二进制文件中剥离的(而不是从子项目的.a 文件)。

修复方法是

  1. 在方案设置中关闭应用程序调试版本的“死代码剥离”。*并
  2. 确保您的单元测试使用调试而不是发布。

(*不要对发布配置执行此操作,因为它会使用从未被调用的代码使应用程序膨胀)。

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

  • App
    • Sub-project
      • A C library embedded (i.e. .c and .h files compiled directly inside)

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

  1. turn off "Dead Code Stripping" for the app's Debug build.* and
  2. make sure your unit tests are using Debug and not Release, in the Scheme settings.

(* don't do this to the release configs as it'll bloat the app with code that is never called).

难得心□动 2024-11-10 01:13:04

如果您的文件位于框架内,则仅应引用导入内的文件夹。否则,将文件添加到项目后,只需执行#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.

迷途知返 2024-11-10 01:13:04

就我而言,主机应用程序是“自定义”。我必须将其更改为 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.

Host Application setting

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文