OpenCV On Mac: How To Get It Up And Running?

opencvOpenCV is a computer vision library used extensively by people in the computer vision field. Until a couple of years ago, OpenCV was a bit hacky and the usage was not very straightforward. But determined efforts by multiple companies finally standardized the process and now it is nice and clean. Computer vision algorithms are computationally intensive, requiring lot of processing power to run in real time. Before OpenCV came along, the efforts were very fragmented and repetitive, and there was no standard library as such. Hence Intel decided to do something about it and came up with OpenCV. The advantage of OpenCV is that the algorithms are highly optimized and the library is available on almost all the popular platforms. I have outlined the procedure below to get OpenCV up and running on your Mac.  


  • Make sure you have git command line tool installed on your machine. If not, I have described the procedure here.
  • Make sure Macports is installed on your machine. If not, you can get it here. Install it before you proceed.
  • If you don’t have cmake, run the following command to install it (this command uses Macports):
     $ sudo port install cmake

Downloading OpenCV

We are now ready to install OpenCV. Download the latest version of OpenCV from here. Make sure you download the version for your platform (Mac, Windows, etc). You will get a directory named opencv-2.x.x, depending on the latest version number.

If you don’t want to worry about downloading every time a new version is released, you can download the OpenCV git repo. This way, you can easily maintain and update your OpenCV version with a simple command. Open the terminal, go to a convenient location and download the OpenCV source code using the following command:

$ git clone git://

It will create a directory called “opencv” and download all the files here. When you want to update to a new version, just go to this folder and type the following command:

$ git pull

This will update your repo to the latest version.

Installing OpenCV

Now that we have the source code for OpenCV, we need to build it. Run the following commands:

$ cd opencv
$ mkdir build
$ cd build
$ cmake -G "Unix Makefiles" ../
$ make -j8

We need to place the libraries in the correct locations so that they are accessible. Run the following command:

$ sudo make install

This will install everything in the /usr/local/ directory. That’s it! OpenCV is now installed on your machine.

OpenCV with Xcode

Now that we have installed OpenCV, we are ready to use it in our C/C++ code. Xcode is the most popular IDE on Mac OS X, so we would naturally want to see how to use OpenCV on Xcode. I have outlined the procedure below:

  1. Open Xcode
  2. Select Xcode -> New -> Project.
  3. A new window will pop up. On the left sidebar, under “OS X”, select “Application” and choose “Command Line Utility”. Click “Next”
  4. In the next window, give a name for the project and make sure the “Type” field at the bottom is set to C++. Click “Next”
  5. In the left sidebar, you should be able to see your project name next to a small Xcode icon. Double click on the project. This should open the Build Settings tab (located on the top of the new window).
  6. In the “Architectures” section, make sure it is set to “Native architecture of the build machine”.
  7. Scroll down to the “Search Paths” section and set “Header Search Paths” to: /usr/local/include. You can do this by double-clicking on the blank space right next to “Header Search Paths” and clicking on the “+” symbol at the bottom left. After you add it, make sure you select “recursive” located on the end of the same row.
  8. Close this window.
  9. In the left sidebar, you should be able to see your project name with a folder icon next to it. If you expand it, you will see main.cpp. Now right click on this project and select “New group”. Name it “OpenCV Libraries”.
  10. Right click on “OpeCV Libraries” and select Add Files to “Project” … (the word “Project” would have been replaced with your project name)
  11. Press the “/” key to get the “Go to” folder prompt. Enter the following path: /usr/local/lib
  12. Select all the files whose names are of the form libopencv_*.dylib.
    Note: Although you don’t have to worry about this, it’s better to know anyway. There will be aliases in that file list as well. So before selecting a file, look at the file icon. If there’s an arrow on the icon, it means that this file is an alias. You don’t have to add these files, since they are just aliases (and not real files).
  13. Click “Add” and you are done!

Go to main.cpp and write a simple piece of OpenCV code. Check if it runs. If you see an error, read on.

Undefined symbols for architecture x86_64

When I first upgraded my Xcode to version 4.6, I got the following error:

"cv::imshow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cv::_InputArray const&)", referenced from:
_main in main1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I didn’t face any such problem in Xcode 4.4. Apparently a build setting seems to default to a different thing in Xcode 4.6. In fact, this started happening from Xcode 4.5 onwards. There is a simple fix for it.

Open Build Settings by double-clicking on the project name with the Xcode icon. In this window, under “Architectures” section, set “Base SDK” to “Current OX S”. Now scroll down to “Apple LLVM compiler 4.1 – Language” section. In “C++ Language Dialect”, change it to “Compiler Default”. In “C++ Standard Library”, change it from “libc++ (LLVM …)” to “libstdc++ (GNU C++ …)”.

That should do the trick for you. If not, there is another way to run OpenCV code. Keep reading!

Running OpenCV Code From Command Line

This is the last sure shot way of running OpenCV code, but you will miss out on the niceness of an IDE. You can use cmake to build and run OpenCV code. Open the Finder and create a folder for your project. Place your .cpp file there. Create a file called “CMakeLists.txt” and place the following lines in that file:

cmake_minimum_required(VERSION 2.6 FATAL_ERROR)

find_package(OpenCV REQUIRED)

# Project Executable
add_executable (filename filename.cpp)
target_link_libraries(filename ${OpenCV_LIBS})

In the above lines, replace “PROJECT_NAME” with your project name and replace filename by your source code file name. For example, if your project name is “OBJECT_TRACKING” and your filename is “tracker.cpp”, then the contents of CMakeLists.txt should be:

cmake_minimum_required(VERSION 2.6 FATAL_ERROR)

find_package(OpenCV REQUIRED)

# Project Executable
add_executable (tracker tracker.cpp )
target_link_libraries(tracker ${OpenCV_LIBS})

Once you create this file, run the following commands in the same directory:

$ cmake .
$ make

You should now be able to see the executable with the same name as the your .cpp file. Now open the terminal and just type the following to run the code:

$ tracker

If you have any command line arguments that you are taking in your .cpp file, give them here:

$ tracker arg1 arg2

And there you go! You are all set to do some OpenCV coding.


19 thoughts on “OpenCV On Mac: How To Get It Up And Running?

  1. Pingback: How To Build A Face Detector? | Perpetual Enigma

  2. Hi, is it possible if you provide some example codes to check if the OpenCV is installed correctly? I’ve got a few sample codes but they dont seem to work. With the error opencv/core.hpp file not found. I’m still trying to troubleshoot. Hope you’ll be able to provide some guide.


      • Thanks for your reply! So if that’s the case do we still need to do the “$ git clone git://” command? Or are there other steps to follow?

      • Running that command will get you the latest build, which you don’t want. So you have to go to the link I sent you in my previous comment, scroll down to the 2.4.3 version and download it. You will see the OpenCV directory once you download. Now you can navigate to this directory from your terminal and run the commands mentioned in my blog post. Hope this helps!

  3. Hi! It started installing properly at first. But the following error pops up:
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    make[2]: *** [lib/libopencv_highgui.2.4.3.dylib] Error 1
    make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
    make: *** [all] Error 2

    Do you know what error i’ve made?

  4. This is the best! It has been nearly impossible to find anyone that can explain, not only how to install, but successfully run OpenCV. Thank you so much for this, it has been so helpful.

  5. I think this is one of the such a lot significant info for me.
    And i’m happy studying your article. But should
    remark oon few general things, The wweb sitre
    style is wonderful, the articlss is in point
    of fact great : D. Goood job, cheers

  6. What Mac version are you using, Because I am having a hard time to get it compiling with 10.9.2. It seems gcc is replaced by clang everywhere now onwards so lot of libraries fail to link. Also compiling cuda support is a challenge. Can you try latest opencv 2.4.8 with mac 10.9.2 ?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s