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?

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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

Comments

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

Comments

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.

2 Comments

hi, i also added -lz, but still got the the same error. I am building on i386. What am i missing?
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.
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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.