Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

ios - Linker errors after upgrading Xcode to 4.5.2 and OpenCV to 2.4.3

My project was working just fine until this morning. I was using xcode 4.3, and an older version of OpenCV (I'm not sure about the exact version). OSX was already 10.7.x, but not 10.7.5

Today, after upgrading OSX to 10.7.5, xcode to 4.5.2, and downloading OpenCV 2.4.3, I am getting the following linker errors when trying to build the project:

Undefined symbols for architecture armv7:
"_OBJC_CLASS_$_ALAssetsLibrary", referenced from:
objc-class-ref in opencv2(cap_ios_video_camera.o)
"cv::FeatureDetector::create(std::string const&)", referenced from:
-[ImageAnalyzer detectBlobs:] in ImageAnalyzer.o
"cv::FeatureDetector::detect(cv::Mat const&, std::vector >&, cv::Mat const&) const", referenced from:
-[ImageAnalyzer detectBlobs:] in ImageAnalyzer.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)

In order to use the new opencv framework I removed the reference to the old framework, and referenced the project to the official prebuilt opencv2.framework downloaded from here.
I also removed the reference to libz.dylib, and added a reference to libc++.dylib instead.
Last step was to update the prefix file to the new framework. The relevant part in the prefix file now looks like this:

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

It all narrowed down to these 4 linker errors I can't seem to get rid of. I tried using libstdc++.dylib, but I am getting even more errors. I also tried building OpenCV myself as explained here, but I am still getting the same errors as the prebuilt framework.

What did I miss? Is there anything else I need to change in my project?


UPDATE:
As seen here, setting the "C++ Standard Library" to "libc++ (LLVM C++ standard libray with C++ 11 support" yielded only one error:

clang: error: invalid deployment target for -stdlib=libc++ (requires iOS 5.0 or later)

Changing the deployment target to iOS 5 finally got my project to run again.
Does this mean OpenCV 2.4.3 doesn't work on iOS versions older than 5?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

steps to compile and run c++ opencv 2.4.4 on mac os x lion 10.7.5 with cmake 2.8.10 and xcode 4.6.1

Having the right tools

  1. download opencv-unix from http://sourceforge.net/projects/opencvlibrary/files/ and untar it wherever
  2. download cmake .dmg from http://www.cmake.org/cmake/resources/software.html and install it
  3. i am assuming you have xcode 4.6 on os x lion which includes the ios sdk 6.1
  4. go to xcode preferences to download and install the Command Line Tools so you have g++ etc.

Use cmake to compile opencv

  1. go to the extracted opencv folder
  2. create a build directory

    mkdir build
    cd build
    cmake -D WITH_TBB=OFF -D BUILD_NEW_PYTHON_SUPPORT=OFF -D BUILD_FAT_JAVA_LIB=OFF -D BUILD_TBB=OFF -D BUILD_EXAMPLES=ON -D CMAKE_CXX_COMPILER=g++ CMAKE_CC_COMPILER=gcc -D CMAKE_OSX_ARCHITECTURES=x86_64 -D BUILD_opencv_java=OFF -G "Unix Makefiles" ..
    make -j8
    sudo make install
    
  3. from the build folder, go to bin/ and run one of the tests

    ./opencv_test_stitching
    

Create your own c++ opencv xcode project

  1. fire up xcode and create a new xcode project
  2. select Command Line Tool for the type of project under os x
  3. open your project's build settings
  4. under Architectures, set Architecture to 64-bit intel. also set Valid Architectures to x86_64
  5. under Build Options, set Compiler for C/C++ to Default Compiler
  6. under Search Paths, set Header Search Paths to /usr/local/include
  7. also under Search Paths, set Library Search Paths to /usr/local/lib
  8. under Apple LLVM compiler 4.2 - Language set C++ Standard Library to libstd++ (For OpenCV 2.4.6, Xcode 5, LLVM 5.0, and 10.8.5, set both language dialect and std library to "Compiler Default" instead of "libstd++")

Add the compiled opencv libraries to your project

  1. go the the Build Phases tab next to Build Settings tab you were in
  2. inside Link Binary With Libraries, click on the + sign and choose Add Other
  3. hit the front slash / on your keyboard and enter /usr/local/lib
  4. hit enter and select the libraries you want to use in your project
  5. make sure you always select libopencv_core.2.4.4.dylib
  6. hit enter and you will see the selected dylibs under your project

write some code

  1. first lets organize the files, right click on your project blueprint icon and select New Group
  2. name the new group opencv or whatever
  3. drag the dylibs and drop them in that group
  4. open main.cpp
  5. copy code from any of the sample tests that came with opencv and paste it here
  6. make sure all the required dylibs are added, for example, if you copied the opencv_test_stitching.cpp code into main.cpp, you will need to add the following libraries in the previous steps libopencv_core.2.4.4.dylib libopencv_highgui.2.4.4.dylib libopencv_stitching.2.4.4.dylib

Cheers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...