Link error while building a unit test target

Unit TestingXcode4Linker ErrorsOcunit

Unit Testing Problem Overview


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?

Unit Testing Solutions


Solution 1 - Unit Testing

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).

Solution 2 - Unit Testing

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.

Solution 3 - Unit Testing

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).

Solution 4 - Unit Testing

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.

Solution 5 - Unit Testing

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncfischerView Question on Stackoverflow
Solution 1 - Unit TestingMartin WickmanView Answer on Stackoverflow
Solution 2 - Unit TestingJoeView Answer on Stackoverflow
Solution 3 - Unit TestingKalleView Answer on Stackoverflow
Solution 4 - Unit TestingJanoView Answer on Stackoverflow
Solution 5 - Unit TestingVladimir VlasovView Answer on Stackoverflow