How to build OpenCV programs in Visual Studio on Windows


I had to build some C++ programs using OpenCV on Windows using Visual Studio (sometimes it's the only option if you're also using some other libraries...). I found that official tutorial on how to build OpenCV applications inside the Visual Studio didn't match versions of the Visual Studio which I had, so it was difficult to apply directly. The way a project is configured and built in VS is very unfamiliar to me, because I mostly use free C/C++ compilers. What is a simple command line flag for GCC, can be an arbitrarily named option somewhere deep in Project Properties dialog of the Visual Studio. cl.exe flags are even harder to guess.

After building several projects and reading tens of questions on Stack Overflow, I've finally compiled the list of steps to take. Most of them require modification of only few fields of the Project Properties.

I assume you start from a new Win32 Console project, disabled precompiled headers and replaced its main .cpp file with whatever OpenCV sample you like.

Well, first things first. Set the compiler and linker flags.

-I, directories where include files are located, are set up in Project Properties -> Configuration Properties -> VC++ Directories -> Include Directores. Click the value, choose , add OPENCV_DIR/build/include, where OPENCV_DIR is where you OpenCV binaries are unpacked.

-L, directories where libraries are installed, are set up in Project Properties -> Configuration Properties -> VC++ Directories -> Library Directores. The same editing procedure. Given usual OpenCV binaries, add OPENCV_DIR/build/x86/vc12/staticlib for Visual Studio 2013, and OPENCV_DIR/build/x86/vc10/staticlib for Visual C++ 2010.

If you're using Express version of the Visual Studio, you probably cannot build 64-bit programs, hence x86. Statically linked libraries (staticlib) seems to be the most painless path with prebuilt OpenCV libraries.

-l, libraries to link, found in Project Properties -> Configuration Properites -> Linker -> Input. Edit Additional Depenendencies value.

The dialog to edit the list of libraries is very spartan. You can simply put there all .lib files from your chosen library directory. For Debug builds use library which end with d, and libraries without d for Release builds. I usually just open my Git Bash, cd to the OpenCV library directory, ls *d.lib, and copy-paste the output (on Windows: select with mouse, Enter, then paste as usual). Obviously, you may skip libraries which you don't use.

If you use HighGUI, you should put comctl32.lib in the list of libraries. Otherwise you'll get LNK2019 "unresolved external symbol __imp__CreateToolbarEx@52" error. If you're using 3.0.0 Beta binaries, which are built with Intel IPP support, you should put also ippicvmt.lib.

Now it's not enough. If you use prebuilt OpenCV binaries, they're built for static runtime. Go to Configuration Properties -> C/C++ -> Code Generation, then change Runtime Library to "Multi-threaded (/MT)" for Release builds or "Multi-threaded Debug (/MTd)" for Debug builds. If you don't do this, you're going to get a bunch of LNK2038 ("mismatch detected for 'RuntimeLibrary'") and LNK2005 errors.

You may wish to define command line arguments and working directory for your program when you run it from within VS. They're configured in Configuration Properties -> Debugging -> Command Arguments and Working Directory respecitively. To keep the console open after the program is finished, run it with Ctrl-F5 rather than F5.

I hope it'll help you to get started.

This setup worked for me with

  • Visual C++ 2010 Express + OpenCV 2.4.9
  • Visual Studio Express 2013 for Windows Desktop + OpenCV 3.0.0 Beta