2

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?

3
  • 1
    Including a file is just like copying and pasting code from one file to another. I'm not sure why you think this would work since they are different languages. I think the question you should be asking is "how can I interface C++ code with objective C code". Commented Jul 18, 2010 at 20:29
  • When using .mm file, Obj-c is just fine including cpp, so I thought the inverse was possible. Commented Jul 18, 2010 at 20:41
  • Yes, but the inverse is including ObjC in a .mm file that mainly uses C++. Commented Jul 18, 2010 at 20:44

3 Answers 3

11

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.

Sign up to request clarification or add additional context in comments.

9 Comments

So interfacing with an objective-c library from c++ is impossible? Hopefully not.
@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: but if I do, seems like I can't use cpp syntax in the .h or .mm file.
@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: 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?
|
3

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).

1 Comment

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.
1

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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.