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 “https://stackoverflow.com/”, 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 “https://stackoverflow.com/” 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 🙂

Leave a Comment