views:

117

answers:

3

Is there a way to include an objective-c header from a cpp? Because when I try to #include "cocos2d.h" from a cpp .h file, a lot of errors complaining about @'s and -'s are showing up.

Can c++ files include obj-c headers like that?

+1  A: 

It is possible when you are compiling with mixed objc/c++. Cocoa applications can be written in languages mix in both directions: you can either use an obj-c class inside the C++ or a C++ class inside a obj-c object.

I assume in your case you are compiling pure C++ app where the obj-c code is not allowed.

Gobra
No, i'm developing for iphone os and the cpp header file is referenced from a .mm file.
sharvey
A: 

C++ has no idea what Objective-C is. So including an Objective-C .h in a .cpp is a no-go.

The other way around, though is fine, if you use the .mm file extension (Objective-C++) instead of .m (Objective-C).

Jon Reid
It would be gross, but if you really really wanted to include a header that includes Objective-C in a .cpp, wrap the Objective-C stuff in `#ifdef __OBJC__` … `#endif` to prevent it from being parsed.
Jon Reid
A: 

It is possible, but you need to use Objective-C++ (e.g. by making the file extension .mm) to mix the languages, plain C++ sources don't work.

To make that clear:

  • .m files only allow Objective-C sources
  • .cpp files only allow C++ sources
  • .mm allow mixed Objective-C++ sources - i.e. both Objective-C and C++ with some limitations

If you need to keep the two worlds seperated instead you need to write wrapper classes that hides the Objective-C details from C++ and vice versa.

Georg Fritzsche
So interfacing with an objective-c library from c++ is impossible? Hopefully not.
sharvey
@shar: Technically you can call into ObjC from plain C++ using the runtime functions - but you'll not want to do that. Just use either the mixed approach or wrap the ObjC classes.
Georg Fritzsche
@Georg Fritzsche: but if I do, seems like I can't use cpp syntax in the .h or .mm file.
sharvey
@shar: In the `.mm` this should be no problem, you might want to ask about that instead with more details. In the header this is fine as long you don't include it in plain `.m` files.
Georg Fritzsche
@Georg Fritzsche: I'm currently using a framework which uses .m files. Might that be the problem, and if yes, is there a way to work around it?
sharvey
@sha: If you are just *using* the framework, there is no problem. You only need `.mm` files where you *mix* the languages, e.g. if you want to call into ObjC methods from C++ code. As i said, show us what is failing.
Georg Fritzsche