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 (I already changed main.m to, the ‘//NSApplicationMain(argc, (const char **) argv);’ is commented out)

  • How do I use Mogenerator?
  • Crashlytics not sending crash Reports to my Fabric dash board?
  • Connect a UILabel in Interface Builder and XCode?
  • Can I install an app to the simulator without the source code?
  • Different storyboard localization per target
  • NSMutableArray Access Issue
  • 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?

    6 Solutions Collect From Internet About “How to include OpenCV in Cocoa Application?”

    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.

    Ian Charnas’s Answer is correct, but there is one modification I would make.

    This article has a more specific solution, and an explanation of why you need to do this.

    // Add this new section BEFORE the #import statements for UIKit and Foundation

    #ifdef __cplusplus
        #import <opencv2/opencv.hpp>

    Even if you rename your “main.m” to “” and moving the “#include opencv2/opencv.hpp” to the top (in the main file), the preprocessor will insert cocoa.h first because of the precompiled header files nemed something like “_Prefix.pch”. To avoid such problems
    – delete the #import statement or
    – insert an #import statement above the cocoa.h import statement

    Try adding: -lstdc++ to the “Other linker flags” in the build settings for your Cocoa app.

    A cocoa application made by the Xcode templates won’t link include the c++ library in it’s settings by default.

    1. add this to your .pch file

      #ifdef __cplusplus
      #import <opencv2/opencv.hpp>
    2. dont forget to convert all of your .m files into .mm files

    You’ll probably find it easier to use OpenCV’s C API rather than the C++ API. You can call C APIs directly from Objective C of course, so this makes life a lot easier. You just lose some of C++’s syntactic sugar, like default parameter values.