How to Compile OpenCV

Posted: May 10, 2012 in C/C++, OpenCV, Programming
Tags: , ,

In this article i will explain to how compile C++ library (OpenCV as an example), how to include the headers, linking the .lib files, and put the .dlls

You can read these two links to know the differences between Static Library ( and Dynamic Library (

This article assumes you have some knowledges in C/C++.  For short, compiling OpenCV and another C++ libraries is always similar and pretty easy after you know how. This tutorial will be in Visual Studio environment (2008 or 2010) .

1. The first step is to install CMake 2.8.x on your computer ( What is this CMake program? It simply prepares the C++ codes to be ready compiled from specified compiler (you know for windows, there are several compilers such as Cygwin, MinGW, Visual C++, etc.). This CMake is really useful to distribute C++ source code so it can be independent from other people’s Compiler and IDE.

2. Then download OpenCV library from sourceforge, the latest version is OpenCV 2.4.0 (

3. Since CUDA is getting popular and OpenCV also implements some of their libraries using CUDA, you can try to install CUDA.  But this is optional since not all computers have NVIDIA GPU. If you need to install CUDA, you can find it here (

4. Unpack OpenCV Installer

5. Open CMake, and set the source directory and where it should create the binaries.

6. Press Configure on the below side, and you should specify your compiler type (mine’s Visual Studio 10 which is specifically for Visual Studio 2010, for Visual Studio 2008 it is Visual Studio 9). and then press Finish

7. Then, the texts will be highlighted red, don’t worry, just press “Configure” again and the red highlight will disappear.

8. Then the last step in CMake is press “Generate”. You should notice “Generating Done” message.

9. Look and on your OpenCV folder and, and you can find .sln file is already generated

10. Click OpenCv.sln to open it with Visual Studio. This time you need to compile OpenCV twice, the first is in Debug mode and the second is Release Mode. Okay, what’s the difference between “Debug” and “Release” mode? For short, Debug mode is fast to be compiled but the performance is slow (unoptimized binary, since you  just need it to check your program is correctly written). As opposite, Release mode is longer in compiling but optimized when it is executed, think it of your program is finished, and ready to be delivered.

11. When you are still compiling (especially in Release mode) you may left your computer to go to toilet or having bath, i am not joking, it will take minutes or hours depends on your computer specification.

12.  If you have done compiling, check out bin folder, you may find .dll files and .lib folder where you can find lib files. Theis bin folder will be divided into two folders, “Debug” and “Release” version.

13. Now it’s time to create your own project, on Visual Studio, File ->  New -> Project -> Visual C++ -> Win32 Console Application (anyway, you may use other project types…)

14. And… right now you need to include the OpenCV headers, right click your Project and choose Properties. A Project actually has two different configuration, “Debug” and “Release”. It means you need to repeat step 15-20 (don’t use All COnfiguration mode since Debug and Release settings are different)

15. Open up Configuration Properties –> C/C++ –> General.

16. you should put these on the Additional Include Directories (absolute path or relative path are both okay)

<your opencv directory>\build\include\opencv;
<your opencv directory>\build\include\opencv2;
<your opencv directory>\modules\core\include;
<your opencv directory>\modules\highgui\include;
<your opencv directory>\modules\imgproc\include;
<your opencv directory>\modules\video\include;
<your opencv directory>\modules\features2d\include;
<your opencv directory>\modules\flann\include;
<your opencv directory>\modules\calib3d\include;
<your opencv directory>\modules\objdetect\include;
<your opencv directory>\modules\legacy\include;
<your opencv directory>\modules\ml\include;%(AdditionalIncludeDirectories)

This is mine, as an example

17. Still in the project properties, open up Configuration Properties -> Linker -> General, look at the “Additional Library Directories”

18. Put these on the “Additional Library Directories”

<your opencv directory>\lib\Debug

Yes actually it’s only one line,  in the Release configuration it should be:

<your opencv directory>\lib\Release

19. Then you need to set up what .lib files we need, open up Configuration Properties -> Linker -> Input, and “Additional Dependencies”

Put this list:


If you curious enough what 240 means it’s the version number, it means OpenCV 2.4.0

20. The last step is put the .dll files on the Debug and Release folders inside your projects, remember, Debug version of .dll  files should be put in Debug folder and Release version .dll files should be put inside “Release” folder, or you’ll get runtime error.

21. Okay, it should be done. don’t forget to repeat step 15-20 for Release mode

22. You can try a “Hello World” code on OpenCV, compile it, if it’s success means you’re done :D

#include "highgui.h"
#include "cv.h"

#include "ml.h"
#include "cxcore.h"

using namespace std;
using namespace cv;

int _tmain(int argc, _TCHAR* argv[])
	IplImage* img = cvLoadImage( "lena.jpg" );
	cvNamedWindow( "Image Window" );
	cvShowImage("Image Window", img);

	cvReleaseImage( &img );
	cvDestroyWindow( "Image Window" );

	return 0;

  1. Thank you for the sharing, I do have two questions:

    1. Do all these compiling steps in vs2010 also valid for vs2008??
    2. does the folder path in “where to build binaries” need to be identical to the one in “where is the
    source code” in cmake??


  2. rizkie says:

    hey, can you give me explaining why mine can’t show the image.
    i follow your steps & it’s success, but the program won’t show the image (show empty image)

  3. Anastasia says:

    Thank you very much for this tutorial. It’s clear and easy to understand.

  4. […] The tutorial to get started with building OpenCV is quite outdated as now the repository changed to GitHub . Luckily the old tutorial still works fine with some trying out here and there - […]

  5. MC says:

    Thank you for perfect instruction.
    I have done it step by step on my comuter and working with it and now the problem is that when I wanted to install it in new computer, in step 10, I have this error:

    The perojects are out of date:

    ZERO_CHECK – Debug Win32
    opencv_gpu – Debug Win32
    opencv_stitching – Debug Win32
    opencv_perf_stitching – Debug Win32
    opencv_perf_gpu – Debug Win32
    opencv_test_gpu – Debug Win32
    opencv_test_stitching – Debug Win32
    opencv_videostab – Debug Win32
    ALL_BUILD – Debug Win32

    and after compiling, there some error on it.

    Would you please help me with that?

  6. I’m not that much of a internet reader to be honest but your
    blogs really nice, keep it up! I’ll go ahead and bookmark your website to come back in the future. All the best

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s