Problem when #import C ++ Header file in iPhone / iPad Project

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

When I compile the project without any #import (from the .h C ++ class), this is normal.

But as soon as I # import the header file in one of the objective-c header files, I get an error, for example:

error: vector: No such file or directory

or

error: expected '=', ',', ';', 'asm' or ' attribute ' to ':' token

I tried setting various values ​​to a file type (C ++ class) in File Info, renaming my objc class to .mm, etc., but it does not work.

So, I must have missed something about importing the .h C ++ class into the objc header file, but that: p ^^

SOLUTION thanks to Vlad
1 Β°) To include a C ++ header file:

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

2 Β°) Rename the objc file to .mm And in its settings file file information file (right clic) as sourcecode.cpp.objcpp

Thanks for the help! Vincent

+6
c ++ iphone compilation xcode
source share
1 answer

Note. Xcode requires that file names have the extension β€œ.mm” for the Objective-C ++ extension to be included by the compiler.

When trying to use C ++ in Objective-C, the code found in a file with the .m extension is the most likely cause of the problem, because the compiler simply does not recognize the C ++ constructs according to the error message. Renaming your .m file to .mm should help.

See Using C ++ with Objective-C for more details.

Assuming you want to use the Objective-C class in the Objective-C ++ source file, there is no problem. The only limitation is that your .h file must be Objective-C clean. This means that you cannot 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 if it is # included in a simple Objective-C file, so it should eliminate any C ++ isms for this situation (1) . Therefore, your Objective-C header file should include the C ++ header, for example:

 #ifdef __cplusplus # include MyCPPHeader.h #endif 
+7
source share

All Articles