Compile OpenCV (2.3.1+) for OS X Lion / Mountain Lion with Xcode
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 :)
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.
If it isn't installed, install Macports and add it to path. (I already had it installed)
Use Macports to update itself from the command line $ sudo port -v selfupdate
Use Macports to install opencv and its dependencies from the command line. $ sudo port install opencv
If it isn't installed, install xCode. (I already had it installed)
Use xCode to create a C++ Command line project.
Use xCode to verify that the simple hello world C++ program it created works by running it in the xCode IDE.
Modify the main.cpp file created by xCode from the hello world example to the simple opencv example from Vachidrewer.
Notice that xCode editer reports that it can NOT find the header opencv header file.
Add /opt/local/include/ to the project search path and notice that the editer errors go away.
Try to run the program in the xCode IDE and notice that it reports that it can't find the opencv libraries.
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.
Use the xCode IDE to run the project and notice that a window pops up with half od it darker then the other half.
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.
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
Create a new XCode project using the Command Line Utility/Standard Tool template. Name it and select C++
Select Project -> Edit Project Settings. Select the Build tab. Set Configuration to All Configurations
In the Architectures section, double-click Valid Architectures and remove all the PPC architectures if any.
Compiler for C/C++/Objective-C > Apple LLVM compiler 4.2 Language" > "C++ Standard Library", and select "libstdc++ (GNU C++ standard library)"
In the Search Paths section set Header Search Paths to /opt/local/include/
Please choose non-recursive as an option while adding that search pathClose the Project Info window
Select Project -> New Group and create a group called OpenCV Frameworks With the new group selected, select Project -> Add files to 'Your Project Name'
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.)
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;
}