## Friday, March 2, 2012

### How to install OpenCV on Mac OS X Lion to work with XCode

I have listed some easy and straight-forward steps below to get OpenCV library working with XCode on Mac OS X Lion without installing unnecessary bulky software on your Mac.

1. Installing CMake

i. CMake is essential to build OpenCV on UNIX. Get CMake Mac OS X binary (dmg) from http://www.cmake.org/cmake/resources/software.html. This comes with a GUI installer, making the installation process easy.

ii. At the end of the CMake installation it will ask whether or not to put CMake into /usr/bin and into PATH environment variable, select Yes and finish the installation.

iii. Open the Terminal (Launch Pad -> Utilities -> Terminal) and make sure that CMake is successfully installed by entering the following command. It should output the CMake version you have installed.
cmake -version

2. Installing OpenCV

iii. Execute the following commands one by one. Each command should complete without errors.

tar xvf OpenCV-2.3.1a.tar.bz2
cd OpenCV-2.3.1
mkdir build
cd build
cmake -G "Unix Makefiles" ..
make
sudo make install

iv. Above commands will build OpenCV into your /usr/local/ directory.

3. Testing a Sample Program with XCode

## Happy coding with OpenCV!

1. when I try to execute the 'make' command, these two errors are generated

make[2]: *** [modules/flann/CMakeFiles/opencv_flann.dir/src/flann.o] Error 1
make[1]: *** [modules/flann/CMakeFiles/opencv_flann.dir/all] Error 2
make: *** [all] Error 2

1. Hi Saravanan,

Sorry I didn't reply sooner. Were you able to solve the problem?

If not, please post the complete error message you get. I could give some help.

2. Sadeep, I was trying to install Open CV in my MAC machine following your instructions. But it was giving 2 errors while executing the 'make' command. Can you please help anyway?

1. Hi Snehasis,

Was the CMake installation successful? Did 'cmake -G "Unix Makefiles" ..' complete without any errors?

Please post the complete error message you get, I might be able to help.

3. Thanks for sharing this, Sadeep! Big help!

1. Glad that you found it useful :)

4. I tried your method in Mac OSX 10.7.5 using OpenCV-2.4.3.tar.bz2, after cmake -G "Unix Makefiles" I get the following error:

OpenCV-2.4.3/build" does not appear to contain CMakeLists.txt

Please let me know how to solve, thanks for the blog post, it is the closest that I had been to install OpenCV :-)

cmake .. -G "Unix Makefiles"

I finished ok the installation with your suggestion, however when I tried the example I got this error:

Undefined symbols for architecture x86_64:
"cv::namedWindow(std::__1::basic_string, std::__1::allocator > const&, int)", referenced from:
_main in main.o
"cv::imread(std::__1::basic_string, std::__1::allocator > const&, int)", referenced from:
_main in main.o
"cv::imshow(std::__1::basic_string, std::__1::allocator > const&, cv::_InputArray const&)", referenced from:
_main in main.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I have no idea about how to solve it!.

Thanks again!

3. This comment has been removed by the author.

4. yeah I've had this problem too. I'm not sure exactly why this happens maybe Sadeep has some insight. I guess the LLVM standard library (LSTL?) doesn't like the openCV API very much.

Here's a workaround that's worked for me so far:
In Build Settings, under "Apple LLVM Compiler 4.1 - Language" choose libstdc++ (GNU C standard library)

On another note,
Thanks Sadeep! This really helped me get going with openCV.

5. Hi,

Assuming that you have added all libs as per 'step vi.)' in my post, I can't figure out the reason for this error message. Please recheck whether those libs are correctly added to your project. Pls try Allen's trick too.

Thanks Allen for the tip. Thanks for the feedback too, glad that I could help. :)

6. this worked for me. Thanks Allen and Sadeep!

I solved the Undefined symbols for architecture x86_64: error.

Now the project load the image!, I will look further how to work with OpenCV

1. No worries. Good to hear that it worked!

2. Thanks all!! This was super helpful!

6. Just a follow-up. I've been having linking errors again (x86_64 blah blah) this time while trying to use cvCvtColor().
The important thing to remember is: import the required libraries!! My problem happened because I didn't import libopencv_imgproc....arrrggghh!!

I've found a bug report on linking errors on OSX that might prove useful if you run into any more linking issues that aren't solved by adding the right libraries:
http://code.opencv.org/issues/2412#note-11

1. Thanks for the follow up Allen!

i tried installing OpenCV by following these steps, however after the cmake -G "Unix Makefiles" .. command the following error occurs

CMake Error: CMake was unable to find a build program corresponding to "Unix Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_C_COMPILER_ENV_VAR
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_C_COMPILER
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_CXX_COMPILER_ENV_VAR
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_CXX_COMPILER
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!

i would appreciate if you could help me out with this

1. I've had a lot of trouble building previous versions of CV (anything below 2.4.3, the current revision). Have you tried it with the latest source?

2. Hi Arihant,

Looking at the error message you posted, it seems that CMake has not been installed properly on your system. Could you please double check your CMake installation? Did you use the GUI CMake installer?

3. Thank you anonymous and Sadeep!,
i was able to successfully build and install OpenCV-2.4.3 on my mac, however when i build the code, another error occurs. This is the error :-

Ld /Users/arihant/Library/Developer/Xcode/DerivedData/firstvisiontest-gcgxreseybtawlanzundevfzylxb/Build/Products/Debug/firstvisiontest normal x86_64
cd /Users/arihant/Desktop/firstvisiontest
setenv MACOSX_DEPLOYMENT_TARGET 10.8
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -L/Users/arihant/Library/Developer/Xcode/DerivedData/firstvisiontest-gcgxreseybtawlanzundevfzylxb/Build/Products/Debug -F/Users/arihant/Library/Developer/Xcode/DerivedData/firstvisiontest-gcgxreseybtawlanzundevfzylxb/Build/Products/Debug -filelist /Users/arihant/Library/Developer/Xcode/DerivedData/firstvisiontest-gcgxreseybtawlanzundevfzylxb/Build/Intermediates/firstvisiontest.build/Debug/firstvisiontest.build/Objects-normal/x86_64/firstvisiontest.LinkFileList -mmacosx-version-min=10.8 -stdlib=libc++ -lopencv_core.2.4.3 -lopencv_core.2.4.3 -lopencv_core.2.4.3 -lopencv_highgui.2.4.3 -lopencv_highgui.2.4.3 -lopencv_highgui.2.4.3 -o /Users/arihant/Library/Developer/Xcode/DerivedData/firstvisiontest-gcgxreseybtawlanzundevfzylxb/Build/Products/Debug/firstvisiontest

clang: error: linker command failed with exit code 1 (use -v to see invocation)

4. Ok..this is fixed now!
i just added the library path and everything was working fine.

Thank you so much Sadeep for a wonderful guide and thanks anonymous for your wonderful tip.

5. If you get the CMAKE_C_COMPILER_ENV_VAR Error, try to install die XCode Comandline Tools from http://developer.apple.com/technology/xcode.html

I have encountered the same problem and I am not sure how could I add the library path as @rih@ant above mentioned.
If anyone could help me with this, I would be very grateful.

7. This comment has been removed by the author.

8. if I have FFMPEG on My MAC ... and I want this openCV connect with it (ffmpeg works under hood the openCV)... can your method do this successfully ??
did you see this in the terminal ?

FFMPEG: YES\n"

1. Unfortunately I don't have access to console logs of the installation now. :(

9. Hey I followed all your steps and I'm using OpenCV 2.4.4 and I got a build error because:

Any suggestions?

1. I had the same problem. Did you ever figure this out?

2. You have to configure a user defined header search path for the compiler that points to the opencv2 directory containing the opencv.hpp file.

Here's how you do it:

1. Open Finder on your Mac and search for the location of the file opencv.hpp (it should be in a directory called opencv2)
2. In xCode, click on the project name in the project navigator to get to the Build Settings
3. In the Build Settings search for "search paths"
4. Change "Always Search User Paths" to Yes
5. Add to "User Header Search Paths" the path to THE DIRECTORY opencv2 you found in step 1. (It should looks something like: /Users/myname/Downloads/opencv-2.4.10/include)

#include

should now work.

10. This is the error i get when i try execting make command... all the previous commands you have given executed successfully
[ 38%] Built target lib tiff

[ 38%] Built target opencv_core

[ 38%] Built target opencv_imgproc

[ 38%] Built target lib jasper

[ 38%] Built target libjpeg

[ 38%] Built target opencv_highgui

[ 38%] Building CXX object modules/flann/CMakeFiles/opencv_flann.dir/src/flann.o

no matching constructor for initialization of 'cvflann::LshIndexParams'
...input_data, const IndexParams& params = LshIndexParams(),

^
candidate constructor not viable: requires 3 arguments, but 0 were
provided
LshIndexParams(unsigned int table_number, unsigned int key_size, ...
^
candidate constructor (the implicit copy constructor) not viable: requires
1 argument, but 0 were provided
struct LshIndexParams : public IndexParams

^

call to function 'operator<<' that is neither visible in the template
definition nor found by argument-dependent lookup
virtual void print(std::ostream& out, void* const* src) { out << ...

^

in instantiation of member function

'cdiggins::anyimpl::big_any_policy::print'

requested here

static typename choose_policy::type policy;

^

in instantiation of function template specialization

'cdiggins::anyimpl::get_policy' requested

here

: policy(anyimpl::get_policy()), object(NULL)

^

'operator<<' should be declared prior to the call site or in namespace

'cdiggins::anyimpl'

inline std::ostream& operator <<(std::ostream& out, const any& any_val)

^
2 errors generated.

make[2]: *** [modules/flann/CMakeFiles/opencv_flann.dir/src/flann.o] Error 1

make[1]: *** [modules/flann/CMakeFiles/opencv_flann.dir/all] Error 2

make: *** [all] Error 2

11. I had an error after cmake -G "Unix Makefiles. I thought cmake wasnt installed correctly. After hours of searching I found:

http://tech.enekochan.com/2012/07/27/cant-configure-with-cmake-in-mac-os-x-10-8-and-xcode-4-4/

It was that I had not installed command line tools in xcode. After installing it the process seems to be working. cross fingers

12. Hi all,

Before starting i'm sorry to post here but i didn't find where else to post this.
I'm quite a begginner with OpenCV since I just Install it on my mac.

And i've a big problem of image size.
when I compile a code I used to work on on Linux, with many "imshow", all the image are really Huge and i can't reduce their size without loosing some information.
(i.e I can reduce the size of the window but if I do it, i can't see all the image inside).
And I don't have this problem on Linux.

Does someone had this problem before ?

Thanks you all ;)

13. hi thanks for the article

i went through the instruction, but got a problem with building the project. 'lopencv_highgui' could not be found.

for Xcode 4.6.3 and opencv 2.4.6.1 it is necessary to add 'usr/local/lib' to LIBRARY_SEARCH_PATHS

find out here: http://opencvexperiments.wordpress.com/2012/11/15/opencvonxcode/

cheers

14. Thanks Sadeep. It was a great help. :)

15. This comment has been removed by the author.

16. hi...Im student from Informatics engineering nice article,
thanks for sharing :)

17. while building the project, there were a few errors : "Use of undeclared identifier 'errno' "...
Thank!

18. Hi there,

could anyone be specific about the problem posted by "NIHAR GOLIMarch 19, 2013 at 12:40 AM" .

i had the similar problem, how can i fix it ??
thanks a lot

19. Hi, I'm trying to install opencv and get the following errors after the make command:

[ 95%] Building CXX object modules/python/CMakeFiles/opencv_python.dir/src2/cv2.cpp.o
In file included from /Users/Oscar/opencv-2.4.6.1/modules/python/src2/cv2.cpp:1:
/Library/Frameworks/Python.framework/Headers/pyport.h:761:2: error: #error "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)."
#error "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)."
^
1 error generated.
make[2]: *** [modules/python/CMakeFiles/opencv_python.dir/src2/cv2.cpp.o] Error 1
make[1]: *** [modules/python/CMakeFiles/opencv_python.dir/all] Error 2
make: *** [all] Error 2

Any chance you know how to fix this? thanks

21. I am new to mac and OpenCV. I have a mac air book Lion. I installed Xcode 4.6.3 and cmake-2.8.12-Darwin64-universal.dmg. I followed all the above steps with no error but some warnings, when I typed -make I received: bash: make: command not found. Please help.

22. Thanks for great tutorial for beginner opencv in Mac. I have trouble with installing cmake -G "Unix Makefiles" .. Please help me to solve that, i have no idea :(
Undefined symbols for architecture x86_64:
"cv::VideoWriter::release()", referenced from:
vtable for cv::VideoWriter in cap.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [lib/libopencv_highgui.2.3.1.dylib] Error 1
make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
make: *** [all] Error 2

23. I have installed the opencv 2.4.6.1 on my Mac book Air with no error. However, when I tried to build the above program, I received an error "user undefined CV_ CV_WINDOW_AUTOSIZE) beside thenfollowing line in the code:
namedWindow("image", CV_WINDOW_AUTOSIZE);

Can you help me?

24. To install OpenCV using the terminal on Ubuntu:
$su - # apt-get update # apt-get install build-essential # apt-get install libavformat-dev # apt-get install x264 v4l-utils ffmpeg # apt-get install libcv2.3 libcvaux2.3 libhighgui2.3 python-opencv opencv-doc libcv-dev libcvaux-dev libhighgui-dev OpenCV should be installed in /usr/local/ Installation Directory You may also want to compile and view examples$ cp -r /usr/share/doc/opencv-doc/examples .
$cd examples$ cd c
\$ sh build_all.sh

Source: http://namhuy.net/1205/how-to-install-opencv-on-ubuntu.html

25. Thanks, it was very helpful.
But i did get an error " library was not found", after adding the /usr/local/lib to library search paths in build settings it was resolved.

26. It's useful

27. Very useful, thank you :) !!!
I was able to complete these steps successfully on OS X 10.8.5 (Mountain Lion) and XCode 5.0.
I ran into a "make Error 2" issue, which was solved by installing command line tools in xcode.
I also ran into "undefined symbols" errors which was worked around by choosing libstdc++ (GNU C++ standard library) as the "C++ Standard Library" under the "Apple LLVM 5.0 - Language - C++" category in the Build Settings.

28. it says "build fail" when i click on project->build on xcode. what should i do? pls help, thanks

1. i mean product->build

29. Thanks for review, it was excellent and very informative.
thank you :)

Im getting the following error.
Determining if the CXX compiler works failed with the following output:

Run Build Command:"/usr/bin/make" "cmTryCompileExec3939479271/fast"
/Volumes/Server HD/Applications/Xcode 2.app/Contents/Developer/usr/bin/make -f CMakeFiles/cmTryCompileExec3939479271.dir/build.make CMakeFiles/cmTryCompileExec3939479271.dir/build
make: /Volumes/Server: No such file or directory
make: *** [cmTryCompileExec3939479271/fast] Error 1

31. Job-Shack–A contractor network site for 1,000 of the largest general contractors looking for sub-contractors in your area .
Building product manufacturers

Thanks for the blog. I have successfully finished all the instruction from " Installing Cmake" to "Installing OpenCV." However, I am stuck in the following steps because there are no results after typing /usr/local/lib in the searching box of adding files window. What might be the cause for this problem? Thank you,

v.) A new file selector window will open. Press '/' key to open Go to folder box. Type /usr/local/lib there and hit Return.

vi.) Select the following files from /usr/local/lib and add them to your project.

libopencv_core.2.3.1.dylib
libopencv_highgui.2.3.1.dylib

33. Great sir, is threre any special steps to follow when it install in windows + eclipse ?

34. I get this error

Ld /Users/tawanda/Library/Developer/Xcode/DerivedData/OpenCVTest-biaarnoqlnwnyiawcxofqtmcdrld/Build/Products/Debug/OpenCVTest normal x86_64
cd /Users/tawanda/sandbox/c++/opencvtest1/OpenCVTest
export MACOSX_DEPLOYMENT_TARGET=10.11