Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Objective–C++ tests using OCMock fail to link #238

Closed
danyowdee opened this issue Sep 14, 2015 · 2 comments
Closed

Objective–C++ tests using OCMock fail to link #238

danyowdee opened this issue Sep 14, 2015 · 2 comments

Comments

@danyowdee
Copy link
Contributor

Issue

When using OCMock in Objective–C++ test classes, linking the tests fails under certain circumstances with an error like the following:

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

The reason for this is simple and can be found in <OCMock/OCMMakeLocation.h>:
extern OCMLocation *OCMMakeLocation(id testCase, const char *file, int line);

This function is declared extern, but not extern "C" as it would need to.

Foundation and the other Apple frameworks solve this by defining a macro like the following:

// From <Foundation/NSObjCRuntime.h>
#if defined(__cplusplus)
#define FOUNDATION_EXTERN extern "C"
#else
#define FOUNDATION_EXTERN extern
#endif

And all exported C functions are subsequently declared this way.

Motivation

As you’re well aware of, Swift is somewhat hostile to mocking. On the C++’s auto keyword, on the other hand, significantly reduces the amount of boilerplate you have to write in an “Objective–C Generics” world.
So using Objective–C++ for tests with OCMock seem like a perfect match; in theory.

In practice, not reliably being able to link your tests depending on inclusion order or something like that is a bit of a downer.

danyowdee added a commit to danyowdee/ocmock that referenced this issue Sep 14, 2015
As detailed in erikdoe#238, linking tests sometimes fails when OCMock is being used in Objective-C++ (.mm) test case files.
This commit solves that problem by conditionally declaring `OCMMakeLocation` as `extern C` in when included in a C++ compilation unit.
@erikdoe
Copy link
Owner

erikdoe commented Sep 16, 2015

Merged #239 now and refactored to using macro as suggested here.

@erikdoe erikdoe closed this as completed Sep 16, 2015
@danyowdee
Copy link
Contributor Author

Thanks for pulling, even after you went the more exhausted route!

I considered the macro wasn't worth it because I only saw this one public function, but it sure does read nicer that way. Sorry for the inconvenience this caused!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants