Compiling OpenCV 2.0 on Snow Leopard

Just a quick info-push to the net. I’ve tried for a while to compile OpenCV 2.0 on Snow Leopard, which always failed with problems in Carbon and Quicktime support of highgui, like so:

opencv/src/highgui/window_carbon.cpp: In function ‘void icvDrawImage(CvWindow*)’:
opencv/src/highgui/window_carbon.cpp:222: error: ‘GetWindowPortBounds’ was not declared in this scope
opencv/src/highgui/window_carbon.cpp:238: error: ‘SetPortWindowPort’ was not declared in this scope

This was using CMake to generate Unix Makefiles to build OpenCV. The solution is simple: Just build it via Xcode! Using

ccmake -G Xcode

on the command line, you can create Xcode project files that will work just fine with Xcode 3.2.

I’ve put an installer here. Requires Snow Leopard and installs to /usr.

[Edit] Of course, this builds for x386 architecture and not x86_64, i.e. the libraries you get are 32bit. This is necessary because Carbon is 32bit. If you need 64bit, you’re out of luck with Carbon.
[Edit] This means you have to add “-arch i386″ to your compile flags.

This entry was posted in Development, Mac. Bookmark the permalink.

40 Responses to Compiling OpenCV 2.0 on Snow Leopard

  1. Tony says:

    Thanks for providing this installer. For some reason, the installed files were placed in /usr/share/opencv and /usr/include/opencv.

    Does this also install the python wrappers? The only python files I found were example scripts.

  2. bjoern says:

    Hmm. I must admit I didn’t try the installer itself — I built the “INSTALL” target in Xcode, which put everything into /Developer/Libraries, and afterwards built the “package” target which built the installer. Strange that the installer puts things in a different place; probably a bug in the OpenCV build scripts. I’ll adapt the blog text.

    Also, I didn’t explicitly do anything about Python, as I’m not using it for CV projects. Sorry.

  3. Marc says:

    Hey

    I have installed the libs via your installer.
    When I try to compile a simple webcam viewer on 10.6 I get this error:
    g++ -o “tess” ./src/tess.o -lcxcore -lcv -lml -lhighgui -lcvaux
    ld: warning: in /usr/lib/libcxcore.dylib, file is not of required architecture
    ld: warning: in /usr/lib/libcv.dylib, file is not of required architecture
    ld: warning: in /usr/lib/libml.dylib, file is not of required architecture
    ld: warning: in /usr/lib/libhighgui.dylib, file is not of required architecture
    ld: warning: in /usr/lib/libcvaux.dylib, file is not of required architecture
    Undefined symbols:
    “_cvCreateCameraCapture”, referenced from:
    _main in tess.o
    “_cvNamedWindow”, referenced from:
    _main in tess.o
    “_cvShowImage”, referenced from:
    _main in tess.o
    “_cvQueryFrame”, referenced from:
    _main in tess.o
    “_cvDestroyWindow”, referenced from:
    _main in tess.o
    “_cvReleaseCapture”, referenced from:
    _main in tess.o
    “_cvWaitKey”, referenced from:
    _main in tess.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    make: *** [tess] Error 1

  4. bjoern says:

    @Marc: What architecture are you compiling for? Try adding “-arch i386″ to your compile flags. Works for me.

  5. Marc says:

    thanks for your quick reply.
    When I add the flag I get the following:

    Building target: tess
    Invoking: MacOS X C++ Linker
    g++ -arch i386 -o “tess” ./src/tess.o -lcxcore -lcv -lml -lhighgui -lcvaux
    ld: warning: in ./src/tess.o, file is not of required architecture
    Undefined symbols:
    “_main”, referenced from:
    start in crt1.10.6.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    make: *** [tess] Error 1

  6. bjoern says:

    Yes. You need to compile your main program with “-arch i386″ as well. Duh ;-)

  7. guisim says:

    Thanks for this ! :)

    I wasn’t able to locate the dylibs after building it using XCode but your installer worked.

  8. guisim says:

    Ok… I can’t compile a simple helloworld program.
    I’m compiling from command line :

    g++ helloworld.cpp -o hello-world -I/usr/include/opencv -L/usr/lib -lm -lcv -lhighgui -lcvaux -arch i386

    and I get :

    Undefined symbols:
    “_cvReleaseImage”, referenced from:
    _main in ccGGqOY8.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status

  9. guisim says:

    I fixed it by adding ” -lcxcore ” to the linker settings ! :)

  10. sulphur says:

    Hi,
    how i can run make install after?? i need to put it to custom location. There is no other way to set architecture ?? through cmake or ./configure ?? I indeed suceed to built is but i’m not sure if cuicktime is working good i tried an copen cv example and i have a carbon window camera is initialized (the green diode is on) however program crashes after without any message :(

  11. sulphur says:

    ok i found the way :)
    CXXFLAGS=”-m32″ LDFLAGS=”-m32″ ./configure –prefix=/xxx works for opencv2.0 on os x 10.6

  12. bjoern says:

    Good one! Thanks for figuring that out!

  13. Pingback: nekonomics

  14. Padilla says:

    Nice job!

    How can I create an Xcode project to be able to debug the code?

    I am now compiling just as guisim posted:

    g++ helloworld.cpp -o hello-world -I/usr/include/opencv -L/usr/lib -lm -lcv -lhighgui -lcvaux -arch i386 -lcxcore

    Thanks in advance.

  15. bjoern says:

    I did the following:

    • Set the build architecture to “32-bit Universal”
    • Put the path to the OpenCV headers into “Search Paths” -> “Other Search Paths”
    • Put “-L$(PATH_TO_CV_LIBRARIES) -lhighgui -lcv -lcxcore” into “Linking” -> “Other Linker Flags”

    That did the job for me.

  16. Dongpyo says:

    Hi,

    After I build opencv libraries from Xcode, I got error in installing.
    Definitely it is because access problem.
    Is there a way to run install in Xcode just like we do this in command line?

    Thank

  17. Blazz says:

    So I downloaded OpenCV 2.0 and did:

    $ mkdir build
    $ cd build
    $ ccmake -G Xcode ../

    Then I opened the Xcode project generated and built it successfully.

    What next? Running make or make install wont work ofc.

    Thanks for the tutorial!

  18. bjoern says:

    @Blazz Well either build the “install” target in Xcode (of course, you need write access to the place you’ve specified as install path when generating the Xcode project file in CMake).

    Or build the “package” target and then install the resulting package with MacOS X’s installer.

    Or just download the installer I provided.

  19. Blazz says:

    I did try downloading your installer but I’m having some issues. For starters when I set the library flags on Xcode it’s not finding the libraries, so I wrote the complete path to each library needed. When I finally got it to work when my simple app runs I get the following warning after which my program crashes:

    warning: Could not find object file “/Users/bjoern/opencvlibrary/trunk/opencv/3rdparty/lib/Debug/libopencv_lapack.a(slasd5.o)” – no debug information available for “/Users/bjoern/opencvlibrary/trunk/opencv/3rdparty/lapack/slasd5.c”.

    So I though it was better for me to compile the code on my Mac and install it :) I’m trying that right now, looking good so far!

  20. Ray says:

    @bjoern
    I was using a python wrapper on top of your provided install:

    arch -i386 python capture-cam.py

    But receive this error:
    OSError: dlopen(/usr/lib/libcxcore.dylib, 6): no suitable image found. Did find:
    /usr/lib/libcxcore.dylib: mach-o, but wrong architecture

    Any ideas?

  21. bjoern says:

    Well, I’ve never used OpenCV with python, sorry. But:

    slytherin:~ bjoern$ which python
    /usr/bin/python
    slytherin:~ bjoern$ arch -i386 python
    Python 2.6.1 (r261:67515, Jul 7 2009, 23:51:51)
    [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import platform
    >>> print platform.machine(), platform.architecture()
    i386 ('64bit', '')

    This suggests that the preinstalled python on Snow Leopard is 64-bit only, and the OpenCV binaries are 32-bit.

  22. mike says:

    I’ve heard there are some wildlife groups trying to get Apple to do more stuff with the actual S.L.’s lol. I don’t know- people are saying it’s good PR for Apple- they should jump on that.

  23. ian says:

    Thanks for the info. I was able to use make with Snow Leopard and OpenCV from the trunk by setting the following using ccmake

    CMAKE_OSX_ARCHITECTURES = i386
    CMAKE_CXX_COMPILER = /usr/bin/g++-4.0
    CMAKE_C_COMPILER = /usr/bin/gcc-4.0

    I wanted to also link with gsl, which required that I install the i386 version of gsl.

    sudo port install gsl +universal

  24. jack says:

    Hi,
    I am new to Mac OS X.
    I have a snow leopard 10.0.6.2

    I tried both your install script (dmg file) and other instructions.
    It didn’t work! I also need video.

    Can you please write instructions more clearly for snow leopard(explain each step from scratch)?

    Thanks…

  25. Wmru says:

    Please can you provide also a version compiled with –with-ffmpeg flag instead of quicktime?
    Ps. Do you know if the quicktime codec is unable to read an mjpeg streaming from an ip camera?

  26. bjoern says:

    Err, no. Sorry. :-) It’s not so hard to compile the thing yourself, though.

  27. mc_plectrum says:

    Hi,
    Thanks for sharing your experience.
    I have build opencv for -i386 with xcode, whats next? Do i have to add someting to: DYLD_LIBRARY_PATH? If yes, which directories?
    At the moment the 3 header files are not found, compiling a new test project( with highgui.h etc. included)

  28. mc_plectrum says:

    @mc_plectrum
    I have now sucessfully build the install target, but the header files cannot be found. What should i do?

  29. mc_plectrum says:

    [SOLVED]
    this post: “jdiamond
    Dec 17, 2008, 11:40 AM
    Thanks so much for your help! Thanks to your link, I found that if you expand an individual target under the Targets list in the project, you see a folder called “Link Binary With Libraries”, which you can right click on and then choose “add existing file.”

    finally solved it…

    this is what i have done before: http://tech.groups.yahoo.com/group/OpenCV/message/68092

  30. Forrest says:

    Thanks, I got compiler successfully on Snow Leopard.

  31. phm says:

    The new python wrappers should be available as “import cv” but the current 2.0.0a build does not create them. How can I build opencv 2.0.0 with the new wrappers? I’ve tried Bjorn’s install, also the ccmake install method on WillowGarage, but I can’t find evidence that it creates them (I could move them to the right place if I could find them).

  32. p3consulting says:

    @bjoern

    python under SL is an universal binary with 3 architectures

    file /usr/bin/python
    /usr/bin/python: Mach-O universal binary with 3 architectures
    /usr/bin/python (for architecture x86_64): Mach-O 64-bit executable x86_64
    /usr/bin/python (for architecture i386): Mach-O executable i386
    /usr/bin/python (for architecture ppc7400): Mach-O executable ppc

    The way you have tested gives the running architecture… which is of course the one of your hardware…

  33. p3consulting says:

    To compile OpenCV 2.0 with Python support under Snow Leopard, you need to do the following changes to the Xcode project generated by cmake -G Xcode :

    1. you have to add the Python framework to the cvpy target, and thus add a “Link binary with Libraries” step to this target
    2. you have to remove of the same target:
    a. the “-framework Python” from the “Other Linker Flags”
    b. the “python” from the “Header Search Path”
    3. you need to change the first line of cv.ccp from
    #include <Python.h" to #include

    and then compiling with current SDK set to 10.6 works.

  34. bjoern says:

    Good one, thanks for the info. As I said, I never did anything with Python and OpenCV… that was the main content of that comment. :-)

  35. realitypixels says:

    Thanks for the tip about compiling in xcode! It worked like a charm — not the first time, though — the compiler crashed — but the second time it actually completed successfully.

    I’m now running the tests. The core passed, and so did some of cv, but there are 14 failures so far, and it seems to get hung up in optflow-estimate-rigid .
    The tests output some cryptic stuff, e.g.:
    moments-hu: ………………………………………….. Ok
     [0m-------------------------------------------------
    [077888f1639ccd45]
    What does this all mean? Does it think that all the tests are taking 32 minutes? Or is the number 32 a result code that represents success, because it seems that 31 represents failure?

    I finally paused the test and it seems that there is a bug in cvfilter.cpp:86:
    p = -p – 1 + delta;
    when
    p == -2147483648
    delta == 1
    then it is stuck in an infinite loop, because
    -(-2147483648) == -2147483648
    in 32 bit 2′s complement arithmetic.

  36. Hi, I have Mac OS X 10.6.2, and have the need to use Quicktime and Carbon.
    I used to build Xcode Framework with the obsoleted build_framework.sh, which had been removed since r2528.

    Now I’m trying to build r2641 with CMake, and your hint made me almost done!
    BTW, I turned off “WITH_1394, WITH_FFMPEG, WITH_UNICAP” in CMake GUI.

    “ALL_BUILD” target worked OK (Well, there’s a C++ bug in acameracalibration_artificial.cpp, line 58 that needs to be changed into “typename” Mat_::const_iterator pos = mat.begin(), …

    However, “install” target gave me this error:

    /opt/local/bin/cmake -DBUILD_TYPE=Debug -P cmake_install.cmake
    – Install configuration: “Debug”
    – Installing: /usr/local/share/opencv/OpenCVConfig.cmake
    CMake Error at cmake_install.cmake:31 (FILE):
    file INSTALL cannot copy file
    “/PATH_TO_OPENCV/opencv/unix-install/OpenCVConfig.cmake”
    to “/usr/local/share/opencv/OpenCVConfig.cmake”.
    make: *** [install_buildpart_0] Error 1
    Command /bin/sh failed with exit code 2

    Again it seemed like an “administrative privilege” problem. I encountered that once and did manual “mkdir” to fix it, but this time I had no idea. Can you tell me how to fix it? (My account IS an administrator)

    And could you please describe again, after “install”, where are those build files? And how do I “use” the library? Like the “PATH_TO_CV_LIBRARIES” in comment #14.

    Thanks in advance!

  37. Stjohn says:

    Hi,

    Thanks for all the tips. I finally got OpenCV-2.0 to work in this way:

    check out the source using svn
    cmake -G Xcode as written above
    Open the Xcode project, set project settings to 10.6 Release, and active architecture i386, build active architecture only on. It should compile just fine.

    For python support to work, and to get rid of the ‘wrong architecture’ error, you have to set up a coupld of things:

    in /Library/Python/2.6/site-packages create a file called OpenCV.pth and inside that file, add the path to your build lib directory.

    Set this environment variable: VERSIONER_PYTHON_PREFER_32_BIT to “yes”

    you can now type ‘import cv’ in your python shell and it doesn’t complain. The sample python files work as well.

    I’m a Houdini geek, I don’t code much, so apologies in advance if this is redundant.

  38. Hernan says:

    @ian

    Thanks Ian. That solved my problem

  39. Yesterday, Cocoa support was checked in to the trunk version of opencv, so you now can compile it as x64. There was only one small change I had to make to compile it: https://code.ros.org/trac/opencv/ticket/165

  40. Vinz says:

    A good way is to use MacPort and do “sudo port install opencv”. Else the best way is to download the last svn version :-)

    If anyone search for an example with video capture : http://www.geckogeek.fr/lire-le-flux-dune-webcam-camera-video-avec-opencv.html

    Good week !
    Vinz

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>