Opencv installed by VCPKG package manager for rapid start of Visual Studio 2022 project

Let’s make our life as Opencv C++ developers easier with the VCPKG package manager. VCPKG is capable of building libraries from source, on demand, using build recipes for specific configurations of Opencv. You may download prebuilt Opencv libraries, build Opencv from git source, and set up your Opencv project in Visual Studio or Cmake manually. You can kind of bypass the configuring process to build Opencv libraries with the VCPKG package manager. VCPKG has direct integration with Visual Studio as well as comfortable usage with CMAKE. The integration will save you time in setting up your project include directories, and configuration of additional library directories and concrete lib dependencies. The great feature is that DLL libraries are copied automatically to the folder with your executable. So, no need to set up some environmental variables for your DLLs locations. It will speed up zero to compile the Hello World Opencv project rapidly in Visual Studio and CMAKE is a topic that needs to be mentioned.

For the CMAKE project with VCPKG, you can read the tutorial here, and the following resources for Opencv cuda and installed by VCPKG can be found on my blog here.

I am still building and configuring OpenCV from source for specific reasons without a package manager, but in some cases, VCPKG is the way to move forward faster. Not all the time is everything perfect, but the package manager is usually faster. So my goal is to get Opencv 4 with GStreamer support library integrated with my C++ project. Let’s play. 

Basics of VCPKG for Opencv project

Vcpkg is a free C/C++ package manager for acquiring and managing libraries. It is under the roof of Microsoft and many open-source developers all over the world. It will download and build a library in single steps. So, You will just choose the configuration and hit the command. 
To start with VCPKG is super simple, You will definitely need to have git installed. Then clone the VCPKG repository to your PC.

git clone

Run this bat file or shell script if you are a Linux user.


You are ready to find the library for your project by search command. So let's list Opencv in the picture below. You can see different configurations for the OpenCV library. I have used different builds of Opencv with FFmpeg and GStreamer support in the past.
./vcpkg.exe search opencv
opencv4[contrib]                          opencv_contrib module
opencv4[cuda]                             CUDA support for opencv
opencv4[cudnn]                            cuDNN support for opencv
opencv4[dc1394]                           Dc1394 support for opencv
opencv4[default-features]                 Platform-dependent default features
opencv4[dnn]                              Enable dnn module
opencv4[dnn-cuda]                         Build dnn module with CUDA support
opencv4[eigen]                            Eigen support for opencv
opencv4[ffmpeg]                           ffmpeg support for opencv
opencv4[freetype]                         Freetype support for opencv
opencv4[gdcm]                             GDCM support for opencv
opencv4[gstreamer]                        gstreamer support for opencv
opencv4[gtk]                              GTK support for opencv
opencv4[halide]                           Halide support for opencv
opencv4[ipp]                              Enable Intel Integrated Performance

By following the install command opencv4[gstreamer] will be built and installed. The default target in my case was the x86-windows platform.

./vcpkg.exe install opencv4[gstreamer]

Computing installation plan...
The following packages will be built and installed:
  * bzip2[core,tool]:x64-windows -> 1.0.8#5
  * bzip2[core,tool]:x86-windows -> 1.0.8#5
  * dirent:x64-windows -> 1.23.2#3
  * dirent:x86-windows -> 1.23.2#3
-- Using cached mingw-w64-i686-pkgconf-1~1.8.0-2-any.pkg.tar.zst.
-- Using cached msys2-msys2-runtime-3.4.6-1-x86_64.pkg.tar.zst.
-- Using msys root at C:/vcpkg/vcpkg/downloads/tools/msys2/6f3fa1a12ef85a6f
-- Installing: C:/vcpkg/vcpkg/packages/opencv4_x86-windows/share/opencv4/copyright
-- Performing post-build validation
Stored binaries in 1 destinations in 18 s.
Elapsed time to handle opencv4:x86-windows: 15 min
Total install time: 2.6 h
If you do not install the meta-port *opencv*, the package opencv4 is compatible with CMake
if you set the OpenCV_DIR *before* the find_package call

    set(OpenCV_DIR "${VCPKG_INSTALLED_DIR}/x86-windows/share/opencv4")
    find_package(OpenCV REQUIRED)

I realize that the target is x86-windows, during the configuration in Visual Studio. I set up a 64-bit debug project and the library was not found. After restart of Visual Studio, You should get your libraries ready to use without any configuration.
So, I rebuilt with x64 bit target opencv4[gstreamer]:x64-windows.

C:\vcpkg\vcpkg>vcpkg.exe install opencv4[gstreamer]:x64-windows  
Total install time: 30 min
If you do not install the meta-port *opencv*, the package opencv4 is compatible with CMake
if you set the OpenCV_DIR *before* the find_package call

    set(OpenCV_DIR "${VCPKG_INSTALLED_DIR}/x64-windows/share/opencv4")
    find_package(OpenCV REQUIRED)

Opencv multiple configurations with VCPKG

To install multiple configurations found in VCPKG is demonstrated in the following command. The FFmpeg,GStreamer are specified in the format of square brackets separated by commas. The recurse is used to prebuild already existing installed libraries. 

vcpkg install opencv4[ffmpeg,gstreamer]:x64-windows --recurse

Visual Studio integration with VCPKG for Opencv

Visual Studio integration is great. I am currently using VS  2022 (64-bit) - Preview Version 17.8.0 Preview 1.0. There is no need to set up an include directory, and no need to set up libraries. You are ready to go after the restart of Visual Studio. There is one prerequisite to issue the VCPKG integrate install command in the following code sample. You are notified with the information "All MSBuild C++ projects can now #include any installed libraries. Linking will be handled automatically. Installing new libraries will make them instantly available."

C:\vcpkg\vcpkg>vcpkg.exe integrate install  
Applied user-wide integration for this vcpkg root.
CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/vcpkg/scripts/buildsystems/vcpkg.cmake"

All MSBuild C++ projects can now #include any installed libraries. Linking will be handled
 automatically. Installing new libraries will make them instantly available.

Now the problem of mixing targets. I initially installed x86 and tried to use this library in the x64 project. So, I have one lesson learned. Created C++ console application, and set target for x64. 

The basic project cannot find the headers, which I was expecting after integrating the install command.

I checked what was installed by default opencv4[gstreamer] and all was x86. You can see the output of the list command below. So the problem can be resolved by changing the target in Visual Studio to Debug target x86 or installing OpenCV in VCPKG with the specific target opencv4[gstreamer]:x64-windows as mentioned in the first section. This chapter includes just more details about the concrete symptoms you can face during the process.

vcpkg.exe list
opencv4[default-features]:x86-windows          Platform-dependent default features
opencv4[dnn]:x86-windows                       Enable dnn module
opencv4[gstreamer]:x86-windows                 gstreamer support for opencv
opencv4[jpeg]:x86-windows                      JPEG support for opencv
opencv4[png]:x86-windows                       PNG support for opencv
opencv4[quirc]:x86-windows                     Enable QR code module
opencv4[tiff]:x86-windows                      TIFF support for opencv
opencv4[webp]:x86-windows                      WebP support for opencv

Check Opencv Build information

With the corrected target, here is a simple program to check the Build information of your Opencv library.

#include <opencv2/opencv.hpp>
#include <iostream>

int main()
    std::cout << cv::getBuildInformation() << std::endl;
    std::cout << "Hello World!\n";

The partial program output includes information about used Opencv library in my project. So you can see that the GStreamer Video IO backend is included.
Video I/O:
    GStreamer:                   YES (1.20.5)
    DirectShow:                  YES
    Media Foundation:            YES
      DXVA:                      YES
What is great about the integration is that Opencv DLLs are copied to a folder, where your Opencv exe file is located. So, no need for any configuration of the linker to handle DLL libraries or global environmental variables.

Summary about VCPKG for Opencv

Vcpkg is a cross-platform package manager for C and C++ development. It allows you to quickly get thousands of high-quality open-source libraries to empower your application including Opencv. VCPKG is capable of building Opencv libraries from source, on demand, using build recipes for specific configurations and platforms. It comes with a curated, tested catalog supporting over 1500 open-source libraries. Users can define their own recipes and bring custom libraries to their VCPKG workflow. The package manager will automatically install or update any missing Opencv dependencies DLLs in your Visual Studio project. 

  • Specific Opencv configuration without deep configuration of CMAKE and collecting dependencies.
  • Start coding without Visual Studio configuration include directories and library dependencies.
  • DLL are copied to the executable folder.
All mentioned benefits, will save you tons of time and debugging of wrong configuration building and setting up your next Opencv project. 

The next article will be about integration of VCPKG with CMake 

Next Post Previous Post
No Comment
Add Comment
comment url