openCV 2.4.3 iOS framework compiler trouble recognising some c++ headers
I have everything working now. After having no joy with the pre-built iOS library available from openCV.org this is what I did...
compile openCV for iOS from a clone of the gitHub repository. Run
build_framework.py
(in theios
folder of the distribution), pointing to an output directory of your choosing. Be sure to have an up-to-date copy of CMake or you will trip over like I did.Your output folder will end up with two subfolders,
build
andopencv2.framework
. Drag the latter into your Xcode project
Add the following line in the project-Prefix.pch
file
#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#endif
(should go above the #ifdef __OBJC__
line)
That is sufficient to get most of openCV working. However it is a very good idea to avoid "objective-C++" (mixing your c++ code in the same files as your objective-C). To manage this you create a thin "wrapper" object (which will be obj-C++) to mediate between your obj-C classes and c++ code. The wrapper essentially has only two roles: to translate data formats (eg UIImage <-> cv::Mat), and to translate between obj-C methods and C++ function calls. See my answer to this question for details (and a github-hosted example project)
To get SURF (and SIFT) working requires a couple of additional steps, as SURF is somewhat deprecated due to licensing issues (it's been moved into nonfree
which does not load automatically).
These includes need to be added in files where you are using SURF
#include <opencv2/nonfree/nonfree.hpp>
#include <opencv2/legacy/compat.hpp>
The code I am working with uses the C interfaces for SURF (eg cvExtractSURF
), so we also need to add this line before calling these functions:
cv::initModule_nonfree();
The other part of my question, how to force Xcode to compile as C++, was a bit of a red herring (there must have been something compatibility issue with the openCV build I was using) - and is no longer required for this solution. However the answer is first, to rename your .m files .mm (for objective-C++) or .cpp (for pure C++) … but if that doesn't work, you can force the issue in the file inspector by changing 'file type'.
update
You also need to take care that the C++ standard library is set correctly in any projects that use the openCV framework. Older versions of openCV (to.2.4.2) want libstdc++
, newer (2.4.3+) expect libc++
. Details here:
https://stackoverflow.com/a/14186883/1375695
update 2
openCV now installs with cocoaPods. To quote SebastienThiebaud
OpenCV is available on Cocoapods. Add one line in your Podfile: pod 'OpenCV'. Pretty easy.
"Pretty easy" ... given all our previous hassles, could be the understatement of [last] year...
Getting Compile Error for opencv 2.4.3
It seems like the comment before that line of code is quite clear. If there is already png.h
on your path, then calls to png.h
might go to the wrong set of code.
To ensure you are using OpenCV's png.h
, they raise an error if there is an alternative png.h
already on your path.
OpenCV 2.4.3+ with libstdc++ for iOS?
In the source for openCV locate this file:
ios/cmake/Modules/Platform/iOS.cmake
Change this line:
set (CMAKE_CXX_FLAGS "-stdlib=libc++ -headerpad_max_install_names -fvisibility=hidden -fvisibility-inlines-hidden")
to:
set (CMAKE_CXX_FLAGS "-stdlib=libstdc++ -headerpad_max_install_names -fvisibility=hidden -fvisibility-inlines-hidden")
Compile using the python script
ios/build_framework.py
Then you should be good to go
I have just tried this on 2.4.3 source, swapped in the resulting framework on an existing project, changed the C++ standard library for the project to libstdc++ and it runs fine.
CVSurf (OpenCV) feature in iOS
You need to read my self-answered question here:
openCV 2.4.3 iOS framework compiler trouble recognising some c++ headers
The bit that is catching you out is probably that SURF has been moved to non-free (as it has licensing issues). So you need to:
#include <opencv2/nonfree/nonfree.hpp>
#include <opencv2/legacy/compat.hpp>
and possibly
cv::initModule_nonfree();
if you are working with older the C interface
There are alternatives to SURF if you wish to stick with the open-licensed standard openCV library...
openCV 2.4.8 iOS 7.0 framework linker errors
Hey I did get it to work, I just had to add the other frameworks that were required to get it to build. You can find them on this page : http://docs.opencv.org/doc/tutorials/ios/video_processing/video_processing.html#opencviosvideoprocessing
How to resolve iOS Link errors with OpenCV
Check this: (assuming you are using LLVM compiler)
Target > Build Settings > Apple LLVM Compiler 4.1 - language > C++ Standard Library
try selecting
libstdc++ (GNU C++ standard library)`
then try switch to
libc++ (LLVM C++ standard library with C++11 support)
libstdc++ seems to work for older builds of openCV, libc++ correct for newer builds. If you have it set wrong (either way) you will see these kinds of errors.
If that isn't the cause, open the build setting side by side in each project and check every setting...
I have been battling through this recently - see my question here, answers here and github sample here. The github project includes opencv framework compiled from current source a few days ago. Right now I am putting together a multi-target sample that links to a different version of the framework if compiling under 10.6/XCode4.2 or 10.7/XCode4.4+. [On github here]
update
As @mikewoz requested, you may need to run current openCV with libstdc++
to remain compatible with other frameworks. It is possible to make a current build with libstdc++
compatibility. For details see my answer to Mike's question here:
OpenCV 2.4.3+ with libstdc++ for iOS?
Linker errors after upgrading Xcode to 4.5.2 and OpenCV to 2.4.3
Since I can't seem to get an answer regarding the versions (neither here nor at the OpenCV Q&A site), I'm going to post this as an answer, as it at least solved the issue. This is described here.
In your project's Build Settings, go down to the section Apple LLVM compiler 4.1-Language.
There:
Set C++ Language Dialect to Compiler Default
Set C++ Standard Libray to libc++ (LLVM C++ standard libray with C++ 11 support
After doing the above, I stopped getting those linker errors, and only got one error instead, which stated that only iOS 5 and above is supported. Changing the Deployment Target to 5.0 in the project summery did the trick.
On a final note, I'm still not sure what it means, regarding OpenCV 2.4.3's compatibility with iOS versions older than 5.
Does OpenCV 2.4.3 for iOS use Accelerate Framework under the hood?
cap_ios.h, as you say, is only used for video and photo capture. This is the only part of openCV that requires the Accelerate framework, so it's use can't go much beyond that. But it is not even required: I am able to run CVVideoCamera video capture using cap_ios.h whether or not the framework is included.
The only reference to it in the docs is
"The Accelerate framework provides some CPU-accelerated DSP filters..."
So... I don't think any significant algorithms are using it. cap_ios.h
is a new feature in 2.4.3 , and the only objective-C interface in openCV, which might explain the hesitant use and mention of the framework.
Related Topics
Std::Remove_If - Lambda, Not Removing Anything from the Collection
Invalid Initialization of Non-Const Reference with C++11 Thread
Function Already Defined Error in C++
Exception Error C0000005 in Vc++
Ptr->Hello(); /* Versus */ (*Ptr).Hello();
If Statement Not Working Right
C++, Std::Atomic, What Is Std::Memory_Order and How to Use Them
Svm Classifier Based on Hog Features for "Object Detection" in Opencv
Do Function Pointers Need an Ampersand
Is Casting Std::Pair<T1, T2> Const& to Std::Pair<T1 Const, T2> Const& Safe
How to Save Two Camera's Data But Not Influence Their Picture-Acquire Speed
Does New Line Character Also Flush the Buffer
How to Convert Platform::String to Char*
Using << Operator to Write to Both a File and Cout