3

I'm trying to make a simple Objective-C++ applicaiton. All of my code is compiling fine, including the use of C++ in Objective-C classes, until I try and add a C++ class to the mix. I've created a simple C++ class:

Test.h

class Test {

};

and included this file in a Objective-C class (with a .mm extension) and I get the following build error:

error: expected '=', ',', ';', 'asm' or '__attribute__' before 'Test'

Clearly I'm missing some simple concept here. I'd appreciate some enlightment.

LandonSchropp
  • 10,084
  • 22
  • 86
  • 149

4 Answers4

2

Well, after scanning more closely over Apple's Documentation, it looks like the answer to to use the __cplusplus preprocessor flag in the header file. Here's what the code looks like now:

#ifdef __cplusplus

class Test {

};

#endif
LandonSchropp
  • 10,084
  • 22
  • 86
  • 149
2

helixed's answer will not help, your class will be just skiped by preprocessor if __cplusplus undefined.

Most of all you trying to include C++ class from *.m file, try to rename it to *.mm. This solve the same problem on my side.

Pavel
  • 920
  • 8
  • 16
2

Shouldn't the preprocessor command required be:

#ifdef __cplusplus
hiddensunset4
  • 5,825
  • 3
  • 39
  • 61
1

In Xcode 6 there is the following option:

Apple LLVM 6.0 - Language -> Compile Sources As

You can set that to Objective-C++ and then the file extension does not matter. This is particularly useful if you are working cross-platform and need to use other file extensions.

sbsmith
  • 600
  • 1
  • 5
  • 16
  • Almost five years later, this was quite helpful! It let me know what setting to look for, even on a later version of xCode. As you say, working on a cross-platform native code app, I was not looking forward to having to rename all my .cpp files to .mm! Thanks so much! – Dronz Dec 25 '19 at 06:47