Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include OpenCV in Cocoa Application?

when I create an xCode project with the 'Command Line Tool' c++ stdc++ template, i am able to include and compile opencv headers and run some code.

But i want to use OpenCV in a 'Cocoa Application' context. When created with that template, i got compile errors when I include the OpenCV headers in main.mm. (I already changed main.m to main.mm, the '//NSApplicationMain(argc, (const char **) argv);' is commented out)

One of those errors is: "Statement-expressions are allowed only inside functions"

I suppose its some kind of compiler version error, but when i compare the project build settings i cant find differences.

Do you have any ideas/expertise?

like image 476
Fabian Avatar asked Sep 28 '10 08:09

Fabian


1 Answers

I ran into the same problem, I spent 2 days doing serious system tracing and as expected, the solution was so simple that an 8-year-old could have found it more quickly than I did.

Ready for this?

In the .mm file where you want to use OpenCV, you need to do your #include "opencv2/opencv.hpp" BEFORE any other includes. That's it! Just move it up a line and watch your problem magically disappear faster than that rug that really tied the room together.

This is for OpenCV 2.2 by the way, hence the new include file. Also if your XCode project uses a prefix header file (look in "Other Sources" for a "YourProjectName_Prefix.pch" file), then you'll need to put your #include "opencv2/opencv.hpp" there instead of in any other file.

like image 71
Ian Charnas Avatar answered Oct 11 '22 08:10

Ian Charnas