How to build OpenCV library and debug it in VS2010?

, Friday 15 February 2013
Imagine you are writing some computer vision code using OpenCV in C++ and you are in one of your routine debugging sessions, the program is suspended on a line that is calling an OpenCV function. You want to step into the OpenCV function, you press F11 but the debugger steps over that line. In this article I am going to show how to build OpenCV from scratch and enable debugging OpenCV inside your own code.

If you are not interested in diving into OpenCV code and you do not wish to understand more about its mechanics you can just follow my Using OpenCV 2.4.x with Visual Studio 2010 tutorial.
Here is what we are going to do. To debug OpenCV we need pdb files, they don't come with the distribution. To obtain them we need to build the library from scratch.

First, download the latest CMake for Windows and install it.

After you launch CMake, show the path of your OpenCV installation as both source code and binary location (I suggest keeping that path simple).

As you can see I am using OpenCV 2.4.4 beta, I am choosing to put the build binaries to the same folder to keep everything together and simple.

After setting the paths, you should press "Configure" button. You will see a dialog asking for your compilers. Since I am compiling my code for 64 bit I select "Visual Studio 10 Win64". You can select "Visual Studio 10" if you are compiling for 32 bit. Make sure "Use default native compilers" radio button is selected. And press "Finish".

If you have Qt installed, this is a great opportunity to enable Qt, check the "WITH_QT" option. If you have no idea, just leave the options as they are, since building OpenCV with Qt is a topic of different article.

Now click "Configure" again. It will do some more work and "Generate" button next to "Configure" will be enabled, click it. It will generate solution and project files for Visual Studio 2010. We are done with CMake, you can close it now.

Navigate into your OpenCV folder, now you can see the generated files. Double click on "OpenCV.sln" to open it in VS2010. Select "Debug" configuration from the dropdown box (hopefully) located top of the VS window, under the menu:

Then click "Build Solution" under the "Build" menu (shortcut is F7). If everything goes well you should see a similar out:

Change the selection to "Release" from "Debug" and build again.

Congratulations you have successfully built OpenCV. Now dll and lib files are under [OPENCV_PATH]\bin and [OPENCV_PATH]\lib respectively.

Now open the project you were developing using OpenCV (assuming you already have some code that you were writing). Modify your project settings according to the new dll and lib paths. I am assuming you already know how to do that since you have a working project. You have to do this because the newly generated pdb files won't fit to dll and lib files that ships with the default distribution.

Next, under "Tools->Options" select "Debugging->Symbols".

Click to the folder icon and write the path to "bin" folder located under your OpenCV path inside the editable area (see figure above). Make sure that path is checked.

You are all done! Now put a break point on a line, and try to step into an OpenCV function!

Inside the infamous imshow() function

1 comment: