0

In my Swift project I use some C++ code, that use openCV framework. So I create Objective-C wrapper and it's works ok. Then I want to use some openCV functionality directly from swift. I add

#import <opencv2/opencv.hpp>

to "Bridging-Header.h" and get bunch of error "Core.hpp header must be compiled as C++" from C++ code. Is there any way to work with OpenCV in C++ code and in Swift code in the same project?

Miki
  • 40,887
  • 13
  • 123
  • 202
FuzzzzyBoy
  • 148
  • 1
  • 13
  • You *cannot* import C++ headers to Swift (as was pointed out to you at https://stackoverflow.com/a/49451486). Using a wrapper is the only approach. – Martin R Mar 27 '18 at 07:42
  • See also [Interacting with C++ classes from Swift](https://stackoverflow.com/questions/35229149/interacting-with-c-classes-from-swift) – Martin R Mar 27 '18 at 07:52
  • Possible duplicate of [swift and c++ function with pointers](https://stackoverflow.com/questions/49451133/swift-and-c-function-with-pointers) – Enea Dume Mar 27 '18 at 08:09

2 Answers2

1

This is not possible to use C++ headers inside bridging header file. Wrap all C++ code by h/mm files

Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
0
  1. integrate OpenCV using pod instead of integrating the library directly.

  2. Create the .pch file and import.

    #ifndef PrefixHeader_pch
    #define PrefixHeader_pch
    #ifdef __cplusplus
    #include <opencv2/opencv.hpp>
    #endif
    #endif /* PrefixHeader_pch */
    
  3. Create the Wrapper file in Objective C file .h and .m and rename the .m to .mm

    Write all logic part in the wrapper file

  4. Create a bridging header file and import the Wrapper file into it.

    #import "OpenCVWrapperr.h"
    
Gaurav
  • 334
  • 6
  • 28