clang: error: linker command failed with exit code

2019-09-09 12:44发布

问题:

I meet this error when I do a unit test on xcode. I try to import "SignIn.h" and test the VerifyEmail class which is defined in SignIn.h/SignIn.m.

My code is as follows:

#import <XCTest/XCTest.h>
#import "SignIn.h"

@interface SignInTests : XCTestCase

@property (nonatomic) VerifyEmail* verifyEmail;

@end

@implementation SignInTests

- (void)testVerifyEmail
{
    _verifyEmail = [[VerifyEmail alloc] init];
}
...

I just follow the usual unit test pipeline. import "SignIn.h" and @property (nonatomic) VerifyEmail* verifyEmail are okay, but when I try to initialize verifyEmail (_verifyEmail = [[VerifyEmail alloc] init];) there is an error as below:

ld: warning: directory not found for option '-Llibs/Debug-iphonesimulator'
Undefined symbols for architecture x86_64:
  "_OBJC_CLASS_$_VerifyEmail", referenced from:
      objc-class-ref in SignInTests.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I have been working on this for a long time and really need someone to help. Thanks!

回答1:

When trying to add unit test to an existing iOS project, sometimes one need to set "Symbols Hidden by Default" in the build settings in the main app target to No. Check this post: http://twobitlabs.com/2011/06/adding-ocunit-to-an-existing-ios-project-with-xcode-4/

It still works for xcode 7. Thanks Todd!