7

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?

Fabian
  • 973
  • 1
  • 11
  • 14

6 Answers6

18

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
  • 366
  • 1
  • 3
  • 8
  • 1
    Even if there is no actual Prefix.pch, the target's build settings might contain a prefix header like the System's UIKit.h for everything. After removing that as well it finally built on my machine. – w-m Feb 02 '11 at 16:28
3

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. http://aptogo.co.uk/2011/09/opencv-framework-for-ios/

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

#ifdef __cplusplus
    #import <opencv2/opencv.hpp>
#endif
user671435
  • 79
  • 1
  • 8
2

Even if you rename your "main.m" to "main.mm" 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

1
  1. add this to your .pch file

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

Mehul Thakkar
  • 12,440
  • 10
  • 52
  • 81
1

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.

petert
  • 6,672
  • 3
  • 38
  • 46
  • hi, i also added -lz, but still got the the same error. I am building on i386. What am i missing? – Fabian Sep 28 '10 at 14:16
  • Why are you commenting out NSApplicationMain()? You'll have to update your question with more specific information, like the exact error message and / or enough of a code snippet for clues to the problem. – petert Sep 28 '10 at 15:10
0

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.

Paul R
  • 208,748
  • 37
  • 389
  • 560