加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 17:43:01 所属栏目:百科 来源:网络整理
导读:我正在使用OCMock 3.0.2,我通过 cocoapods安装了我的测试目标: platform :ios,'7.0'xcodeproj 'myProject.xcodeproj'target :myTestTarget do pod 'OCMock','~ 3.0.2'endlink_with "myTestTarget" 在我的测试文件(myTest.mm)中,我已经包含了OCMock并希望尝
我正在使用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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读