openCV 2.4.3 iOS framework compiler trouble recognising some c++ headers

后端 未结 2 433
[愿得一人]
[愿得一人] 2020-12-01 19:14

openCV 2.4.3 / Xcode 4.5.2 / mac osx 10.8.2

I am trying to get openCV working with iOS. I am attempting to use the the prebuilt 2.4.3 framework from openCV.org. H

相关标签:
2条回答
  • 2020-12-01 19:33

    openCV implementation class just add .m to .mm as implementation file.

    #import <UIKit/UIKit.h>
    #import <opencv2/opencv.hpp>
    
    @interface ViewController : UIViewController
    
    @end
    

    enter image description here .mm file run in C++ compiler in iOS, so it not show error.

    0 讨论(0)
  • 2020-12-01 19:45

    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 the ios 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 and opencv2.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...

    0 讨论(0)
提交回复
热议问题