OCMock 3.0.2 linker error with .mm test file

人走茶凉 提交于 2019-12-05 14:54:07

OCMMakeLocation is declared like this

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];
}

It's a straight C function defined outside of the Objective-C interface. I'm not knowledgeable enough about what the complier is actually doing (maybe someone else can explain it better), but to the best of my understanding, this is what's going on: Your test file is an Objective-C++ file, so it's getting complied with C++ linkage, which does name mangling (see this about name mangling). However, OCMLocation is compiled as a Objective-C file, so it gets C linkage, not C++ linkage, so no name mangling. Because your test is complied with C++ linkage, it pulls in OCMock.h and assumes it's also a C++ header, so it assumes the result of compiling it's source will be the same, which it's not going to be.

Long story short, to fix this, all you need to do is tell the compiler that OCMock.h is a C header in your test file:

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

It looks like you are building OCMock from source, you're building it for x86_64 (64-bit) only, and then try to use the binary in a project that is i386 (32-bit). Using the pre-built binaries available from the downloads page should fix your issue because these binaries are fat, they contain i386 and x86_64.

It could be the case that Cocoapods is trying to do a Debug build, which has "only active args" set to true. I don't know enough about Cocoapods to say how to force it to do a Release build, which should create fat binaries.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!