sharvey
sharvey

Reputation: 8185

include objective-c header in c++ file

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?

Upvotes: 2

Views: 7413

Answers (3)

Georg Fritzsche
Georg Fritzsche

Reputation: 99122

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.

Upvotes: 11

Jon Reid
Jon Reid

Reputation: 20980

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

Upvotes: 3

Gobra
Gobra

Reputation: 4261

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.

Upvotes: 1

Related Questions