c – 带有.mm测试文件的OCMock 3.0.2链接器错误

前端之家收集整理的这篇文章主要介绍了c – 带有.mm测试文件的OCMock 3.0.2链接器错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用OCMock 3.0.2,我通过 cocoapods安装了我的测试目标:

platform :ios,'7.0'
xcodeproj 'myProject.xcodeproj'

target :myTestTarget do
  pod 'OCMock','~> 3.0.2'
end

link_with "myTestTarget"

在我的测试文件(myTest.mm)中,我已经包含了OCMock并希望尝试新的就地验证策略,如下所示:

- (void) test_myTest
{
    MyObject *obj = [MyObject new];
    id robotMock = OCMPartialMock(obj);

    [obj testMethod];

    // some asserts
    OCMVerify([obj _internalMethodToBeCalled]);
}

到目前为止似乎正常.但是,当我尝试运行此特定测试用例时,我收到链接错误

Undefined symbols for architecture i386:
  "OCMMakeLocation(objc_object*,char const*,int)",referenced from:
      -[MyTests test_myTest] in MyTests.o
ld: symbol(s) not found for architecture i386
clang: error: linker command Failed with exit code 1 (use -v to see invocation)

我确认已正确引入OCMock并且也引用了OCMLocation.h / m文件.我看到OCMMakeLocation似乎是一个外部函数,但.m文件作为我的pods构建目标中的依赖项目存在,但不知何故它没有被链接.我必须让我的测试成为.mm,因为我包含了一些c文件.为什么这会成为OCMock的问题?

解决方法

OCMMakeLocation是这样声明的

OCMLocation.h:

extern OCMLocation *OCMMakeLocation(id testCase,const char *file,int line);

OCMLocation.m:

OCMLocation *OCMMakeLocation(id testCase,const char *fileCString,int line)
{
    return [OCMLocation locationWithTestCase:testCase file:[NSString stringWithUTF8String:fileCString] line:line];
}

它是在Objective-C接口之外定义的直接C函数.关于编译器实际上做了什么(或许其他人可以更好地解释),我不够了解,但据我所知,这就是正在发生的事情:你的测试文件是一个Objective-C文件,所以它正在变得越来越好符合C连接,它确实命名为mangling(see this about name mangling).但是,OCMLocation被编译为Objective-C文件,因此它获得C链接,而不是C链接,因此没有名称重整.因为你的测试符合C链接,它会引入OCMock.h并假设它也是一个C头,所以它假定编译它的源的结果将是相同的,它不会是.

简而言之,为了解决这个问题,您需要做的就是告诉编译器OCMock.h是测试文件中的C头:

#ifdef __cplusplus
extern "C" {
#endif
#import <OCMock/OCMock.h>
#ifdef __cplusplus
}
#endif

猜你在找的Xcode相关文章