compiling a #include in Xcode - error during compilation: file not found

前端 未结 1 1544
不思量自难忘°
不思量自难忘° 2021-01-15 18:04

I have added one framework in my project, where to get the callbacks of framework I need to implement c++ code,for which I have added c++ classes which are .mm from .cpp

相关标签:
1条回答
  • 2021-01-15 18:28

    I think problem is that your's .h-file, where you have added C++ includes is included in other .m-files, not just in .mm. There is several ways to solve this problem:

    1. Use C++ includes only in .mm files. (don't use them in .h)
    2. Change all files, that import yours header to .mm
    3. Include them in

      #ifdef __cplusplus
      #endif
      

    block. Also use this block for methods where C++ classes mentioned. For example:

    #ifdef __cplusplus
    #include <string>
    using namespace std;
    #endif
    
    @interface SomeClass
    
    #ifdef __cplusplus
    - (void) setName:(const string&) name;
    #endif
    
    @end
    

    Also there is way to avoid #ifdef #endif block by using extensions and categories:

    1. declare C++'s instance variables only in extensions.
    2. create categories with additional .h-file for methods and properties, that use C++'s types.
    0 讨论(0)
提交回复
热议问题