Reputation: 9596
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.
When I uncomment the using namespace cv; I get the following:
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 ...
Upvotes: 20
Views: 13063
Reputation: 2543
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:
Tested on Xcode 11.6 OpenCV Ver 3.4.3
Upvotes: 3
Reputation: 56
Try placing #import < opencv2/opencv.hpp >
before #import "ViewController.h"
.Source
Upvotes: 1
Reputation: 446
I just had the exact same problem. I'm working in a Swift project with OpenCV.
Regarding Swift, its entry point to OpenCV is a file I called OpenCVWrapper. So I got OpenCVWrapper.h and OpenCVWrapper.mm. In the bridging header of my project, I got #import "OpenCVWrapper.h".
Thing is I wanted to write a class called MatUtils in Objective-C++ that I could call from OpenCVWrapper.mm. For them to be seeable in there, I had to put them in MatUtils.h.
Long story short, the mistake is that in OpenCVWrapper.h, I did #import "MatUtils.h". MISTAKE!!!! As OpenCVWrapper is in the bridging header, C++ is now reachable from Swift!
Quick fix : #import "MatUtils.h" in OpenCVWrapper.mm!
Cheers! bRo
Upvotes: 16
Reputation: 94654
OpenCV is a C++
framework, which means that any code that makes use of OpenCV has to be compiled with C++
interpretation, rather than C
interpretation.
The errors you see, e.g. with the using namespace cv;
indicate that the code is compiled using the objective-C compiler, rather than the objective-C++ compiler.
As I mentioned in my comment the easiest way to get this to happen is to ensure that any file that #include
s an opencv header must be named e.g. ViewController.mm
, i.e. it must be an Objective-C++ file.
Alternatively, you can select and override the Type
of the file, by explicitly selecting the Objective-C++ Source
option for the file type in the utilities pane.
Upvotes: 31