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

前端 未结 1 2009
后悔当初
后悔当初 2020-12-31 23:16

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

相关标签:
1条回答
  • 2020-12-31 23:42

    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
    
    0 讨论(0)
提交回复
热议问题