Xcode 7 and openCV (no Swift): Core.hpp header must be compiled as C++

Viewed 11249

I have followed the instructions on how to install OpenCV on an iOS project. However when using Xcode 7 I had to add manually a prefix header. Doing this unfortunately did not help and I was still getting compile errors. I then read another post suggesting that is best to add manually the imports and not use prefix headers in Xcode 7, so I did.

Here is my code:

#import "ViewController.h"

#import <opencv2/opencv.hpp>
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <opencv2/highgui/cap_ios.h>
//using namespace cv;

@interface ViewController ()
{
    IBOutlet UIImageView* imageView;
    IBOutlet UIButton* button;
}

- (IBAction)actionStart:(id)sender;

@end

However I still get the following errors.

enter image description here enter image description here

When I uncomment the using namespace cv; I get the following:

enter image description here

I found some complex solutions talking about exposing headers to Swift etc.. I just want my project to work on Objective-C with Xcode 7 ...

4 Answers

The solution is almost the same as we do bridging ObjectiveC and swift but we do it between Objective C and C++ so we put the C++ codes in a separate file with (.mm) extension and then import it to your Objective C File (.m)

For example if you have ViewController.h and ViewController.m you add two more files which include your C++ codes lets name it OpenCV.mm and OpenCV.h then import the OpenCV.h inside ViewController.m even before you import anything else. May be this image will help understand it: enter image description here

Tested on Xcode 11.6 OpenCV Ver 3.4.3

Try placing #import < opencv2/opencv.hpp > before #import "ViewController.h".Source

Related