8

I have an opencv project that I've been developing under ubuntu 12.04, on a parellels VM on a mac which has an x86_64 architecture. There have been many screen switching performance issues that I believe are due to the VM, where linux video modes flip around for a couple seconds while camera access is made by the opencv application. I decided to moved the project into Xcode on the mac side of the computer to continue the opencv development. However, I'm not that familiar with xcode and am having trouble getting the project to build correctly there.

I have xcode installed. I downloaded and decompressed the latest version of opencv on the mac, and ran: ~/src/opencv/build/cmake-gui -G Xcode .. per the instructions from willowgarage and various other locations. This appeared to work fine (however I'm wondering now if I'm missing an architecture setting in here, although it is 64-bit intel in Xcode). I then setup an xcode project with the source files from the linux project and changed the include directories to use /opt/local/include/... rather than the /usr/local/include/... I switched xcode to use the LLVM GCC compiler in the build settings for the project then set the Apple LLVM Dialog for C++ to Language Dialect to GNU++11 (which seems possibly inconsistant with the line above)

I'm not using a makefile in xcode, (that I'm aware of - it has its own project file...)

I was also running into a linker issue that looked like they may be resolved with the addition of this linker flag:

   -lopencv_video  

based on a similar posting here: other thread however in that case the person was using a Makefile in their project.

I've tried adding this linker flag under "Other Linker Flags" in xcode build settings but still get build errors.
I think I may have two issues here, one with the architecture settings when building the opencv libraries with Cmake, and one with the linker flag settings in my project.

Currently the build error list looks like this:

    Undefined symbols for architecture x86_64:
    "cv::_InputArray::_InputArray(cv::Mat const&)", referenced from:
    _main in main.o
    "cv::_OutputArray::_OutputArray(cv::Mat&)", referenced from:
    _main in main.o
    "cv::Mat::deallocate()", referenced from:
    cv::Mat::release()    in main.o
    "cv::Mat::copySize(cv::Mat const&)", referenced from:
    cv::Mat::Mat(cv::Mat const&)in main.o
    cv::Mat::operator=(cv::Mat const&)in main.o
    "cv::Mat::Mat(_IplImage const*, bool)", referenced from:
    _main in main.o
   "cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)", referenced from:
   _main in main.o
   ---SNIP---
   ld: symbol(s) not found for architecture x86_64
   collect2: ld returned 1 exit status

Can anyone provide some guidance on what to try next?

Thanks, Walt

Community
  • 1
  • 1
Walt
  • 117
  • 2
  • 15

1 Answers1

15

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 EDIT: successfully tested with opencv 2.4.5 on mac os x mountain lion 10.8.3 and Xcode 4.5

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

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++ (With OpenCV 2.4.6, Xcode 5, LLVM 5.0, and 10.8.5, set both language dialect and std library to "Compiler Default" instead)

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.

  • Thankyou verymuch...this solved my problem..the problem was with "C++ Std Library".By default it was "libc++". Changing it to "libstdc++" has done the trick. Thankyou verymuch once again – tez Apr 19 '13 at 11:55
  • Great tutorial got me further than most others. Any change this has changed with xcode 5 and OpenCV 2.4.6.1? I get some errors during sudo make install but I'll try to track them down on their own. Thanks! – Dave Collins Oct 23 '13 at 03:03
  • According to this comment(http://stackoverflow.com/questions/14030546/linker-errors-after-upgrading-xcode-to-4-5-2-and-opencv-to-2-4-3/15662948#comment28843244_15662948), yes there are changes. "With OpenCV 2.4.6, Xcode 5, LLVM 5.0, and 10.8.5, I had to set both language dialect and std library (step 8) to "Compiler Default". Otherwise I'd see linker errors (complained about imwrite specifically), or missing ." I updated the answer. Hope it works for you. –  Oct 24 '13 at 21:49