Compiling opencv statically on OSX

OpenCV is rapidly changing and there is no bundled version that comes with OSX. I’m working on some command line apps and don’t feel like explaining to my users how to install Macports so I decided I would need to statically compile it into my application. There is a OSX Framework available, but I’m not making an application bundle so that was a no-go.

Here is what needed to be done:

First off, this document on the OpenCv Wiki doesn’t seem to work anymore if you are pulling directly from subversion.

Build System – Open CV seems to have 3 types of build scripts in subversion: Make, Autotools, and CMake. It took a lot of trial and error before I realized that only CMake appears to be working, at least on my system.

You can get a copy of CMake here here.

Before you run CMake, you have to change the following files:

  • src/cxcore/CMakeLists.txt
  • src/highgui/CMakeLists.txt
  • src/cvaux/CMakeLists.txt
  • src/cv/CMakeLists.txt
  • src/ml/CMakeLists.txt
  • tests/cxts/CMakeLists.txt

Look for the add_library lines and change this:

add_library(${the_target} DYNAMIC ${lib_srcs} ${lib_hdrs})

to this:

add_library(${the_target} STATIC ${lib_srcs} ${lib_hdrs})

Once you have made that change, follow the CMake instructions here

In Your Own Project
Assuming the above worked well, you can now statically compile your application pointing to the .a files generated by your opencv build.

One thing to keep in mind, is that your application now has to be linked to OSX system frameworks. You can resolve this by adding the frameworks to your linker in your Makefile: