OpenCV C++ Setup for Microsoft Visual Studio(2019) and Visual Studio Code on Windows 64-bit Platform

Divyendu Narayan
5 min readNov 14, 2019

--

I am starting with the premise that you have some experience of setting up development tools for C++. You have already installed 7-zip, Python3(64 bit) (use latest, https://www.python.org/downloads/release/python-374/), Windows-Git( https://git-scm.com/download/win),CMake for Windows( https://cmake.org/download/), Microsoft Visual Studio 2019(Desktop Development with C++) and Visual Studio Code( https://code.visualstudio.com/download). In section (1) I will show you how to build OpenCV Library from source using CMake and Visual Studio 2019. In section(2) I will show Visual Studio Solution to display image “HelloWorld.jpg”. If you are not planning to use Visual Studio then you can go to section (3) which deals with setting up Visual Studio Code.

(1) Building OpenCV Libraries :

  • next start cmake
notice the location for source code and the location where binaries need to be built.
  • click on “Configure”
configure generator and platform

in the window that pops up
— select the generator as Visual Studio 16 2019
— Platform as x64(as we are working on 64-bit platform)
and then click finish
(if you have not installed Python and setup environment variable, then you will get errors, if you get any error then don’t proceed, try to resolve or send the snapshot of error in comments)

  • once configuration is done, click “Generate”
  • click on “Open Project”. if you have installed MS Visual Studio 2019 with Desktop Development for C++ then it will launch MS Visual Studio

make sure to select target as “Release” and platform as “x64”

  • click on “Build Solution”

When build is successful you should be able see above. Estimated build time is 30 mins

(2) Setup Microsoft Visual Studio 2019 C++ solution for OpenCV

c:\>git clone https://github.com/divy-works/opencv_cpp_windows_setup.git

Explorer view after git clone,

  • Open a Project or Solution
C:\opencv_cpp_windows_setup\visual_studio_2019_setup\OpenCVVisualStudio2019.sln

Let’s go through the settings:

  • Open Project “Properties”:
  • Configure field “Additional Include Directories” with the location of OpenCV header files
C:\opencv-4.1.2\opencv\build\include
  • Optional setting for the target name and location
  • Configure Linker for the path to OpenCV lib files and the lib files

configure field “Additional Library Directories” with the location of OpenCV Lib files

C:\opencv-4.1.2\opencv\build\x64\vc19\lib\Release
  • Conffigure Linker-> input -> Additional Dependencies to include the OpenCV lib files at the location (C:\opencv-4.1.2\opencv\build\x64\vc19\lib\Release)
ade.lib                
opencv_core412.lib
opencv_features2d412.lib
opencv_gapi412.lib
opencv_imgcodecs412.lib
opencv_ml412.lib
opencv_photo412.lib
opencv_stitching412.lib
opencv_video412.lib
opencv_calib3d412.lib
opencv_dnn412.lib
opencv_flann412.lib
opencv_highgui412.lib
opencv_imgproc412.lib
opencv_objdetect412.lib
opencv_python2.lib
opencv_ts412.lib
opencv_videoio412.lib

update “path” environment variable to include the location of the OpenCV Binary,

C:\opencv-4.1.2\opencv\build\x64\vc19\bin\Release

we are done with the settings, next we can build the application “image_viewer.exe”

Next build the solution as show below. Make sure the target is “release” and platform is “x64”.

  • in the explorer notice, image_viewer.exe once build is complete
  • to test the binary
C:\opencv_cpp_windows_setup>visual_studio_2019_setup\image_viewer.exe HelloWorld.jpg

you should observe following:

(2) Setup Visual Studio Code for OpenCV

Start Application “x64 Native Tools Command Prompt for VS2019”. This application comes with Visual Studio (Desktop Development with C++) installation.

notice the environment is initialized for platform “x64”

next clone https://github.com/divy-works/opencv_cpp_windows_setup

(1) git clone https://github.com/divy-works/opencv_cpp_windows_setup(2) cd opencv_cpp_windows_setup\visual_studio_code_setup(3) code . 
(This launches Visual Studio Code at the current location)

Let’s go through Visual Studio Code settings

  • .vscode\c_cpp_properties.json
contains configurations to include OpenCV header files and msvc compiler
  • .vscode\tasks.json
contains configurations to include the source file, OpenCV Header files, Lib file location and lib files
  • update path environment variable to include the location of the OpenCV Binaray,
C:\opencv-4.1.2\opencv\build\x64\vc19\bin\Release
  • for building image_viewer.exe run ctrl + shft + B

As a result of build, image_viewer.exe is created

To test the application, run following:

references:

(1) the image_viewer.cpp code is from, Learning OpenCV 3, Computer Vision in C++ with the OpenCV Library, Authors: Gary Bradski, Adrian Kaehler

--

--