Building and using OpenCV with Eclipse on Mac OS X

This took me a little while to figure out due to the sometimes subtle differences in Mac OS X and Linux, and the imperfect documentation.  Eventually I got it working though. The goal was to build OpenCV for Mac OS X 10.9 and then get it working in Eclipse. So, here goes:

  1. Ensure you’re using a JDK with version number 1.7 or higher.  If not, see here for instructions.
  2. Download the latest version of OpenCV (I used 2.4.8, so if you have problems, try going back this) from here, for Mac.
  3. Extract it.
  4. cd into the extracted directory of OpenCV in your terminal and execute the following:
mkdir build
cd build
cmake -G "Unix Makefiles" -D CMAKE_CXX_COMPILER=/usr/bin/g++ -D CMAKE_C_COMPILER=/usr/bin/gcc -D WITH_CUDA=ON ..
make -j2

(Note in the above, -j2 refers to the number of CPU cores I had – e.g. set to -j4 if you have 4 logical cores).

Then just look in build/bin, and you’ll see a .jar at the bottom – congratulations, you just built OpenCV! Import this as a library into Eclipse, and set the native path also to build/bin, and you’re done.

If you get stuck, let me know in the comments and I’ll do my best to help.