0

i create a view controller viewsampleViewController.. And it has two method setname and getname.. i create a testcase for that view controller. my testcase name is newTestCase and method name is testName.

#import "newTestCase.h"
#import "viewsampleViewController.h"


@implementation newTestCase

in my testName method,

-(void)testName{

 NSString *b=@"hello";
 v =[[viewsampleViewController alloc] init];
 STAssertNotNil(v,@"v doesnt created");
 [v setuname:@"hello"];
 NSString *a=[v getuname];

 STAssertEquals(b,a,@"error:name not equal");
 [v release];

}

-(void)setUp{
 v=[viewsampleViewController alloc];
}

-(void) tearDown{
 [v release];
}

when i build i got an error

 Ld "build/Debug-iphoneos/Unit test.octest/Unit test" normal armv6
cd /Users/anande/Documents/viewsample
setenv IPHONEOS_DEPLOYMENT_TARGET 3.1.3
setenv PATH "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.2 -arch armv6 -bundle -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.1.3.sdk -L/Users/anande/Documents/viewsample/build/Debug-iphoneos -F/Users/anande/Documents/viewsample/build/Debug-iphoneos -F/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.1.3.sdk/Developer/Library/Frameworks -filelist "/Users/anande/Documents/viewsample/build/viewsample.build/Debug-iphoneos/Unit test.build/Objects-normal/armv6/Unit test.LinkFileList" -dead_strip -framework Foundation -framework SenTestingKit -miphoneos-version-min=3.1.3 -o "/Users/anande/Documents/viewsample/build/Debug-iphoneos/Unit test.octest/Unit test"


Undefined symbols:
  "_OBJC_CLASS_$_viewsampleViewController", referenced from:
      __objc_classrefs__DATA@0 in newTestCase.o
ld: symbol(s) not found

collect2: ld returned 1 exit status

plz help me,

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
Almand
  • 333
  • 2
  • 4
  • 9

5 Answers5

19

This linker problem appears because of architecture and dependency.Actually when we add some files to our project from some other sources then there may not establish the exact dependency.So compiler generates the linker error.I solved this problem by following the steps:

  1. Select Target
  2. Select build phases
  3. Select compile sources
  4. Choose + icon
  5. Select the .m file for which you are getting error and add it.
  6. Clean the build and run again.
carbonr
  • 6,049
  • 5
  • 46
  • 73
14

This is a Linker error, add viewsampleViewController.m to your test target. Please start your class names uppercase, makes everything a lot more readable

Martin Brugger
  • 3,168
  • 26
  • 20
  • thanks martin.. when i add my .m file to test target it works.. thanks alot.... but martin. in apple tutorial has describe that once i add dependencies, then no need to add .m file to test target. i add dependencies. then why not it works – Almand Dec 24 '10 at 04:38
  • If you are developing a static library, then you can add your library as an "Other Linker Flag" build setting on the test bundle target. However, you are testing an application viewController so you have to include the implementation file in the test target for compilation. It can't link to the application bundle. For static libs, you prefix your library with -l. To tell the compiler to look for a static lib named MyStaticLib.a, add "-lMyStaticLib" to the "Other Linker Flags" in the test target build settings. – levous Jul 27 '11 at 02:54
  • You **don't** have to include your implementation files in the test target — even if you're making an application bundle. See http://twobitlabs.com/2011/06/adding-ocunit-to-an-existing-ios-project-with-xcode-4/ – jemmons Mar 13 '14 at 03:35
  • I think the original answer was only valid until Xcode 3.x Afterwards Apple change the build system. – Martin Brugger Mar 14 '14 at 14:02
2

Somehow the viewsampleViewController.m file is not being compiled. Make sure its part of the project.

jamihash
  • 1,900
  • 1
  • 12
  • 15
  • thanks martin.. when i add my .m file to test target it works.. thanks alot.... but martin. in apple tutorial has describe that once i add dependencies, then no need to add .m file to test target. i add dependencies. then why not it works – Almand Dec 24 '10 at 04:37
0

I had the same problem, in my doing some changes on the sources of the project. All the .m were changed to .mm and for some reason in the 'Link Binary With Libraries' the specific class that was having problems wasn't added there, but was in 'Copy Bundle Resources'. After I changed that it worked.

So this is what i did:

1) Click on the blue project tab on the upper-left side of Xcode. 2) Go to the 'TARGETS' option. 3) Go to 'Build Phases' Tab. 4) Go to 'Copy Bundle Resources'. 5) Look for the implementation file (.m, .mm) and if it's there remove it by clicking on the minus (-) option. 6) Go to 'Compile Sources' and click on the plus (+) option and add that implementation file. 7) Compile and hit it, brother, you're good to go.

Hope this helps. Cheers.

Felipe
  • 498
  • 10
  • 29
0

While adding your app's .m files directly to your testing target solves the problem, it's redundant and unnecessary. Follow the steps outlined here by Two Bit Labs to get it working. To sum up, make sure your…

  1. test target's Bundle Loader build setting is pointing at your app's bundle.
  2. test target's Test Host build setting is pointing at your app's bundle.
  3. app target's Symbols Hidden by Default build setting is NO.
jemmons
  • 18,605
  • 8
  • 55
  • 84