开发者

Using C++ objects from Obj-C / Obj-C++ -- Calling external methods

I'm using C++ objects in an Obj-C code. To do this, I've wrapped the C++ objects in ObjC objects, which in turn use opaque pointers to access the C++ classes. That stuff is working.开发者_JAVA百科

the Problem: The C++ objects (e.g. CPPObj.cc) use methods from additional c++ header files (e.g. ToolsCPP.h)---when I try to compile the ObjC code (e.g. Obj.mm), it complains that those methods are undefined.

Even if I include ToolsCPP.h directly in the Obj.mm file, it still complains.

Ugly Solution: Include all of the necessary methods from ToolsCPP.h in CPPObj.cc, so that the C++ objects are fully self-contained. This would be a huge pain.

Is there any better / easier / proper way to do this?

Thanks!


It sounds like you want to pack your ToolsCPP code (and any other C++ dependencies) into a library and then link it with your Obj-C code.


I don't know the exact error you're seeing, but I suspect it could be that you need to wrap your C-based headers with extern "C" like this in your .mm files:

extern "C" {
  #import "foo.h"
}

More info here: Why do we need extern "C"{ #include <foo.h> } in C++?


you either have to wrap what you need (e.g. using objc methods), or use objc++ to allow the client to use the c++ types appropriately.


Since this is apparently a linker error, it means that the code from "ToolsCPP.cc", i.e. the code that corresponds to ToolsCPP.h, is not being compiled and linked with the rest of your project.

Check that ToolsCPP.cc is included in your project and confirm that it is actually being compiled.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜