Home > Development, Mac > Compiling OpenCV 2.0 on Snow Leopard

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.

Categories: Development, Mac Tags:
  1. Tony
    October 3rd, 2009 at 19:11 | #1

    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. October 3rd, 2009 at 20:50 | #2

    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
    October 5th, 2009 at 21:13 | #3

    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. October 5th, 2009 at 21:20 | #4

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

  5. Marc
    October 5th, 2009 at 21:38 | #5

    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. October 5th, 2009 at 21:46 | #6

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

  7. October 6th, 2009 at 00:13 | #7

    Thanks for this ! :)

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

  8. October 6th, 2009 at 00:41 | #8

    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. October 6th, 2009 at 02:21 | #9

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

  10. sulphur
    October 6th, 2009 at 18:09 | #10

    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
    October 6th, 2009 at 19:35 | #11

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

  12. October 6th, 2009 at 22:52 | #12

    Good one! Thanks for figuring that out!

  13. Padilla
    October 16th, 2009 at 05:44 | #13

    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.

  14. October 17th, 2009 at 16:06 | #14

    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.

  15. Dongpyo
    October 20th, 2009 at 12:20 | #15

    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

  16. Blazz
    October 22nd, 2009 at 18:07 | #16

    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!

  17. October 22nd, 2009 at 18:35 | #17

    @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.

  18. Blazz
    October 23rd, 2009 at 10:59 | #18

    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!

  19. Ray
    November 1st, 2009 at 23:35 | #19

    @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?

  20. November 2nd, 2009 at 08:55 | #20

    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.

  21. mike
    November 3rd, 2009 at 22:18 | #21

    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.

  22. ian
    November 5th, 2009 at 23:39 | #22

    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

  23. jack
    November 12th, 2009 at 20:34 | #23

    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…

  24. Wmru
    November 16th, 2009 at 22:13 | #24

    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?

  25. November 16th, 2009 at 22:26 | #25

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

  26. mc_plectrum
    December 2nd, 2009 at 00:49 | #26

    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)

  27. mc_plectrum
    December 2nd, 2009 at 09:54 | #27

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

  28. mc_plectrum
    December 2nd, 2009 at 19:40 | #28

    [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

  29. Forrest
    December 10th, 2009 at 15:04 | #29

    Thanks, I got compiler successfully on Snow Leopard.

  30. phm
    December 24th, 2009 at 16:05 | #30

    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).

  31. p3consulting
    December 27th, 2009 at 15:37 | #31

    @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…

  32. p3consulting
    December 27th, 2009 at 16:17 | #32

    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.

  33. January 6th, 2010 at 16:37 | #33

    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. :-)

  34. January 29th, 2010 at 13:07 | #34

    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!

  1. October 14th, 2009 at 17:38 | #1