Compile OpenCV (2.3.1+) for OS X Lion / Mountain Lion with Xcode

15,546

Solution 1

Detailed guide how to get OpenCV 2.3.1 up and running under OS X Lion (10.7.2) with Xcode 4.2.1 using MacPorts

EDIT 08/06/2012: This is also working for OpenCV 2.4.1. Just make sure you got the latest version of Xcode and installed the "Command Line Tools" (Xcode -> Preferences -> Downloads -> Command Line Tools).

EDIT 15/08/2012: Tested everything with Mountain Lion ans the current versions of Xcode & OpenCV … it's working :) And you don't have to use the LLVM compiler.

EDIT 16/10/204: Over the last year I abandoned MacPorts and started using brew, which works better for my purposes.

Brew guide

1.) Get the current Version of Brew here.

2.) Make sure brew is ready to us

brew doctor && brew update 

3.) Install OpenCV (as of 17/20/2014 v2.4.9)

brew install opencv

4.) Fire up Xcode (as of 17/20/2014 v6.0.1) and open/create your project

5.) Select your target, go to "General" and hit the "+"-Button in the "Linked Frameworks and Libraries"

5.1.) Click "Add Other", hit "/", go to "/usr/local/lib" and add any libopencv_**.dylib you need

6.) Now add "/usr/local/include" to your "Header Search Paths" under "Build Settings" (target still selected)

7.) Finally make sure include OpenCV in your .mm files.

MacPorts guide (maybe outdated)

1.) Get the current Version of MacPorts here. Don't forget to add "/opt/local/(s)bin" to your environment PATH

export PATH=/opt/local/bin:/opt/local/sbin:$PATH

2.) Keep your MacPorts up-2-date:

sudo port -v selfupdate

3.) Install OpenCV 2.3.1 (building with llvm-gcc)

sudo port install opencv configure.compiler=llvm-gcc-4.2

4.) Fire up Xcode and create your project

5.) Select your target, go to "Summary" and hit the "+"-Button in the "Linked Frameworks and Libraries"

5.1.) Click "Add Other", hit "/" and go to "/opt/local/lib"

5.2.) Add any libopencv_**.dylib you need

6.) Now add "/opt/local/include/" to your "Header Search Paths" under "Build Settings" (target still selected)

7.) Finally make sure to have the following lines at the beginning of your .pch file:

#ifdef __cplusplus
  #import "opencv2/opencv.hpp"
#endif

Otherwise you'll get some nasty erros like this:

"Non-const static data member must be initialized out of line"
"Statement expression not allowed at file scope"

That's it! Hope it helps :)

Solution 2

Thanks to Vachidrewer, I was able to get OpenCv running on Mavericks. I did things in a little different order,so here are my notes.

  1. If it isn't installed, install Macports and add it to path. (I already had it installed)

  2. Use Macports to update itself from the command line $ sudo port -v selfupdate

  3. Use Macports to install opencv and its dependencies from the command line. $ sudo port install opencv

  4. If it isn't installed, install xCode. (I already had it installed)

  5. Use xCode to create a C++ Command line project.

  6. Use xCode to verify that the simple hello world C++ program it created works by running it in the xCode IDE.

  7. Modify the main.cpp file created by xCode from the hello world example to the simple opencv example from Vachidrewer.

  8. Notice that xCode editer reports that it can NOT find the header opencv header file.

  9. Add /opt/local/include/ to the project search path and notice that the editer errors go away.

  10. Try to run the program in the xCode IDE and notice that it reports that it can't find the opencv libraries.

  11. Add a group called opencvfrqmework to the project and add /opt/local/lib/libopencv_core.dylib and /opt/local/lib/livopencv_highgui.dylib to the group.

  12. Use the xCode IDE to run the project and notice that a window pops up with half od it darker then the other half.

Solution 3

Have you tried just building it using standard UNIX Makefiles?

Follow this guide, and see if that helps. If you have already downloaded the source code, you probably don't need to do the svn checkout that is suggested. You can probably start with Use CMake to build in section 2.

Hope that helps.

Solution 4

With small changes to @moosgummi answer below steps worked with Xcode 4.6 on Mac OSX 10.7 TEST code is included below.

Installation of OpenCV :

Get the current Version of MacPorts here.

Don't forget to add "/opt/local/(s)bin" to your environment PATH

export PATH=/opt/local/bin:/opt/local/sbin:$PATH

Keep your MacPorts up-2-date:

sudo port -v selfupdate

Install OpenCV with mac ports

sudo port install opencv

Configuring Xcode for using OpenCV

  1. Create a new XCode project using the Command Line Utility/Standard Tool template. Name it and select C++

  2. Select Project -> Edit Project Settings. Select the Build tab. Set Configuration to All Configurations

  3. In the Architectures section, double-click Valid Architectures and remove all the PPC architectures if any.

  4. Compiler for C/C++/Objective-C > Apple LLVM compiler 4.2 Language" > "C++ Standard Library", and select "libstdc++ (GNU C++ standard library)"

  5. In the Search Paths section set Header Search Paths to /opt/local/include/
    Please choose non-recursive as an option while adding that search path

  6. Close the Project Info window

  7. Select Project -> New Group and create a group called OpenCV Frameworks With the new group selected, select Project -> Add files to 'Your Project Name'

  8. Press the "/" key to get the Go to the folder prompt. Enter /opt/local/lib Select libopencv_core.dylib, libopencv_highgui.dylib (you may need to add other library files from this folder to run other code.)

  9. Uncheck Copy Items… and click Add

TEST CODE

Copy this code into your main.cpp file. It should open up a small window that is half shaded.

#include <iostream>
#include <opencv2/opencv.hpp>

int main(int argc, char *argv[])
{
    // Open the file.

    IplImage *img = cvCreateImage( cvSize(100,200), IPL_DEPTH_8U, 3); //if (!img) {

    //    printf("Error: Couldn't open the image file.\n");
    //    return 1;
    //}

    // Display the image.
    cvNamedWindow("Image:", CV_WINDOW_AUTOSIZE);
    cvShowImage("Image:", img);

    // Wait for the user to press a key in the GUI window.
    cvWaitKey(0);
    // Free the resources.
    cvDestroyWindow("Image:");
    cvReleaseImage(&img);

    return 0;
}
Share:
15,546
dom
Author by

dom

Full-time geek enthusiastic about longboards &amp; code written in js, c++ or objective-c

Updated on June 11, 2022

Comments

  • dom
    dom almost 2 years

    Can anyone provide me some detailed guide how to compile OpenCV 2.3.1 on OS X Lion with Xcode?

    I'm getting mad about this … I got the source, used cmake to create the Xcode template and tried to build it, but it fails with about 200 errors.

    Thanks in advance, Dom

    SOLUTION in my answer post.

  • Pavan K
    Pavan K about 12 years
    during installation ffmpeg failed to install. Do you know any workaround?
  • Barney Szabolcs
    Barney Szabolcs over 11 years
    To point 7: do rather #include<opencv2/core/core.hpp> etc where necessary to reduce compiling-code bloat.
  • Barney Szabolcs
    Barney Szabolcs over 11 years
    To point 1: if you don't add the export the export path you can drag in the apropriate dylib files from /opt/local/lib to your project.
  • Barney Szabolcs
    Barney Szabolcs over 11 years
    To point 3: install tbb first to get a faster version of opencv. (sudo port install tbb then add +tbb to opencv)
  • Bharat
    Bharat about 11 years
    Your link is broken. The working link is here: guidefreitas.com/…
  • Patricia
    Patricia over 9 years
    I want to create a project that builds opencv2.framework using Xcode6 for iOS only. Do you know how to do that, b0ts? Or do you know where I can find instructions to do so? Thank you!
  • l'L'l
    l'L'l over 8 years
    Link only answers are useless when the links don't even work.