6

I have a C++ class I would like to use in an iPhone/iPad project.
I created this file in different ways (like with "New File" => C++) and the error is always the same.

When I compile the project without having any #import (of the .h C++ class), it's ok.

But as soon as I #import the header file in one of my header objective-c file, I get error like :

error: vector: No such file or directory

or

error: expected '=', ',', ';', 'asm' or 'attribute' before ':' token"

I tried setting different values to the file type (of C++ class) in File Info, renaming my objc class in .mm etc, but it doesn't seem to work.

So I must have missed something about importing the .h c++ class in the objc header file, but what :p ^^

SOLUTION thanks to Vlad
1°) To include the header c++ file :

#ifdef __cplusplus
    #include "Triangulate.h"
#endif

2°) Renaming the objc file in .mm AND in his File Info (right clic) setting file type as sourcecode.cpp.objcpp

Thanks for helping !
Vincent

Vinzius
  • 2,890
  • 2
  • 26
  • 27
  • Are you sure that your Objective-C++ files has `.mm` extension? –  Oct 08 '10 at 12:50
  • Yeah I tried #include with/without the .mm extension in the implementation file. It doesn't change at all the errors. The c++ source codes are these one : http://www.flipcode.com/archives/Efficient_Polygon_Triangulation.shtml – Vinzius Oct 08 '10 at 12:53
  • Oh, man. `.h` file in ObjC headers is a different story. I extended my answer, please see the last paragraph. –  Oct 08 '10 at 12:59

1 Answers1

8

Note: Xcode requires that file names have a “.mm” extension for the Objective-C++ extensions to be enabled by the compiler.

Trying to use C++ in Objective-C code residing in a file with .m extension is the most probable cause of the problem because compiler just does not recognize C++ constructs according to the error message. Renaming your .m file to .mm should help.

For more details, see Using C++ with Objective-C.

Assuming you want to use an Objective-C class in an Objective-C++ source file, there's no problem at all. The only restriction is that your .h file must be Objective-C clean. This means that you can't use any C++-isms in it, or that if you do you must wrap them all in #ifdef __cplusplus. The header will be compiled in ObjC mode when it's #included by a plain Objective-C file, so it has to eliminate any C++isms for that situation (1). So your Objective-C header file should include C++ header like this:

#ifdef __cplusplus
# include MyCPPHeader.h
#endif
  • As I said in my question, I tried. And it doesn't work. Maybe I missed something. – Vinzius Oct 08 '10 at 12:58
  • See the last paragraph I've just added. If you mix `.m` and `.mm`, you have to wrap C++ inclusions so that `.m` files will not see C++ included in Objective-C++. –  Oct 08 '10 at 13:04
  • My My My. I tried things but it doesn't work :-( I have read all the cocoadev.com page and maybe my solution is to recode all the c++ class in objc -_- I tried using "#ifdef __cplusplus" then in the .mm the compiler doesn't recognize the new c++ class -_- All without thinking that after I need to use this .mm class in objc basic files. So first solution, I forget about using c++ here xD second solution I get a miracle xD – Vinzius Oct 08 '10 at 13:17
  • If I wrap the include with "#ifdef __cplusplus", then I don't get error. But if I use a c++ class name then in the .mm, I get error saying my object doesn't exist :/ I tried wrapping it too, but nothing is called during the run :/ (I put a NSLog), any idea ? – Vinzius Oct 08 '10 at 13:28
  • Yeah, it looks like you messed up with stuff... It should work out of the box if none of those problems exist. Try to rename `.h` file into `.hpp` maybe? Who knows, maybe your compiler treats `.h` header as C source instead of C++ :) IDK... –  Oct 08 '10 at 13:30
  • @Vinzius: It means __cplusplus is not defined even though file has `.mm` extension. That is weird. Try going trough project settings and see what could possibly prevent Xcode from treating `.mm` files right. –  Oct 08 '10 at 13:38
  • It works now ^^ thanks. It wasn't the hpp but setting the .mm as .cpp.objcpp in File Info (AND doing #ifdef __cplusplus #include "Triangulate.h" #endif) THANKS ^^ It compile without error (for the moment :p) – Vinzius Oct 08 '10 at 13:43
  • It is weird Xcode didn't change meta info when you renamed `.m` into `.mm`. Anyways, if it works it works! Congrats! –  Oct 08 '10 at 13:57
  • It seems to be a severity 1 bug in Xcode4. I have this situation in a trivial case and there is NOTHING that fixes it :(. – Adam Aug 24 '12 at 05:48
  • thanks man. It took me half a day to find what I need (this answer :D) and only 45 seconds to solve the problem after reading it. +1 for this. – x4h1d May 14 '13 at 15:25