5
votes

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?

7
You might want to check out this post, Mach O Link Error- linker command failed with exit code 1 (use -v to see invocation), for your linker error. Let us know how it goes.Douglas
OK I just updated the question. Changing the C++ Standard Library, and setting the deployment target to 5 did the trick. Does that mean that the new OpenCV versions don't support iOS versions older than 5?Darkshore Grouper

7 Answers

6
votes

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.

4
votes

It seems that your project is missing the framework AssetsLibrary.

  1. Select the top node in the project navigator. (The project and targets page with the build settings appears.)

  2. Select the target.

  3. Select Summary.

  4. Scroll down to Linked Frameworks and Libraries.

  5. Click the Plus icon at the end of the table and select AssetsLibrary.framework.

  6. Click Add.

Then try to build it again.

4
votes

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.

1
votes

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

GoTo BuildSettings. Set c++ standard library to compiler default. The error will get removed surely.

1
votes

Just to get this into the postings somewhere in case someone else runs into the same thing. If you follow all the great advice aboutsetting the proper c++ library to link against for building an iOS app BUT still get the link errors for undefined symbols make sure your code files are set to compile as c++! That is rename your .m to .mm and .h to .hpp. It's the little things...

0
votes

Instead of using terminal commands given in the opencv installation guide in official website, use the following commands to build opencv from terminal. Worked for me.

cd OpenCV-2.3.1

mkdir build

cd build

cmake -G "Unix Makefiles" ..

make

sudo make install

0
votes
  1. Go Xcode/General/Linked Frameworks and Libraries
  2. Press "+" button
  3. type: AssetsLibrary
  4. Select "AssetsLibrary.framework" and import it

Done

Good luck!