Using @import in objective C in conjunction with __cplusplus

后端 未结 2 1357
小蘑菇
小蘑菇 2020-12-31 13:05

When I try to compile an Objective C++ file (.mm) that is linked to a file that uses the new @import directive, I get some errors. Currently, my only solution is to replace

相关标签:
2条回答
  • 2020-12-31 13:47

    Edit: According to the latest docs this should work now.

    See this. You have to use the -fcxx-modules flag instead of the -fmodules flag, but as the official documentation suggests - EXPERIMENTAL and VERY BROKEN. I didn't manage to get it working at all... In my opinion it's better to stick with #import and #include until that feature is out of experimental stage.

    From clang.llvm.org:

    -fcxx-modules
        Enable the modules feature for C++ (EXPERIMENTAL and VERY BROKEN).
    
    0 讨论(0)
  • 2020-12-31 14:02

    In my case I needed to use OpenCV in my application.

    I have solved this problem by adding

    -fcxx-modules (Objective C++) manually to "Other C++ Flags"

    (Build Settings/ Apple Clang - Custom Compiler Flags/ Other C++ Flags)

    or

    -fmodules (Objective C) to "Other C Flags"

    (Build Settings/ Apple Clang - Custom Compiler Flags/ Other C Flags)

    And also I needed to put import the opencv.hpp before any Apple's headers to fix errors of Expected identifier in opencv library. For example in Prefix.pch I organized it that way:

    #ifdef __cplusplus
    #include <opencv2/opencv.hpp>
    #endif
    
    #import <Availability.h> 
    

    I am using:

    • Xcode - 11.3
    • CocoaPods - 1.9.1
    • OpenCV2 - 4.3.0
    0 讨论(0)
提交回复
热议问题