Opencv installed by VCPKG package manager for CMAKE project

 This is the second chapter of the series about the VCPKG package manager focus on CMAKE integration. The first part here was about the installation of VCPKG and integration with Visual Studio 2022. So for basics, like installation of VCPKG, search for packages and install the right target. Please have a look at part one

Video tutorial here

CMAKE Opencv project with VCPKG package manager

Now, jump to CMAKE using just VS code. The user experience from CMAKE depends on your experience, we need to pay attention to what CMAKE tells us if there is a problem. Let's assume that my VCPKG is located in Directory: C:\vcpkg\vcpkg. The structure of my sample project in the opencv_p1 directory is in the following image. Inside my project directory is one source code file main.cpp, the CMakeLists.txt, and the build directory, where the program will be compiled and linked.

My CMakeList.txt file content: 
cmake_minimum_required(VERSION 3.15)
project(opencv_p1 CXX)

find_package(OpenCV REQUIRED)
find_package(protobuf REQUIRED)

add_executable(main main.cpp)
target_link_libraries(main PRIVATE ${OpenCV_LIBS} protobuf::libprotoc
protobuf::libprotobuf protobuf::libprotobuf-lite)
The configuration based on CMakeList.txt is performed from the build directory in the following format. The -DCMAKE_TOOLCHAIN_FILE needs to point to vcpkg.cmake location.
  C:\cmake_tut\opencv_p1\build>cmake ..
"-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/vcpkg/scripts/buildsystems/vcpkg.cmake"
As, I have both 2019 and 2022 Visual Studio, By default Cmake creates a project for Visual Studio 16 (2019). Let's change this to VS 2022

Cmake Opencv generated by Visual Studio 17 2022

Let's try to configure the project again. I clean the build directory. You can simply delete the content of the build directory and recreate the configuration. So, the -G generator will be VS 17, and -A architecture x64. Now, The following command will configure my project again.
build> cmake .. "-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/vcpkg/scripts/buildsystems/vcpkg.cmake"
 -G "Visual Studio 17 2022" -A x64
-- The CXX compiler identification is MSVC 19.38.33030.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Preview
/VC/Tools/MSVC/14.38.33030/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Protobuf: C:/vcpkg/vcpkg/installed/x64-windows/tools/protobuf/protoc.exe
(found version "3.21.12.0")
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - not found
-- Found Threads: TRUE
-- Found TIFF: optimized;C:/vcpkg/vcpkg/installed/x64-windows/lib/tiff.lib;debug;
C:/vcpkg/vcpkg/installed/x64-windows/debug/lib/tiffd.lib (found version "4.5.1")
-- Found OpenCV: C:/vcpkg/vcpkg/installed/x64-windows (found version "4.7.0")
CMake Warning (dev) at C:/Program Files/CMake/share/cmake-3.27/Modules/
FindPackageHandleStandardArgs.cmake:438 (message):
  The package name passed to `find_package_handle_standard_args` (Protobuf)
  does not match the name of the calling package (protobuf).  This can lead
  to problems in calling code that expects `find_package` result variables
  (e.g., `_FOUND`) to follow a certain pattern.
Call Stack (most recent call first):
  C:/Program Files/CMake/share/cmake-3.27/Modules/FindProtobuf.cmake:652
(FIND_PACKAGE_HANDLE_STANDARD_ARGS)
  C:/vcpkg/vcpkg/installed/x64-windows/share/protobuf/vcpkg-cmake-wrapper.cmake:16
(_find_package)
  C:/vcpkg/vcpkg/scripts/buildsystems/vcpkg.cmake:809 (include)
  CMakeLists.txt:10 (find_package)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Found Protobuf: optimized;C:/vcpkg/vcpkg/installed/x64-windows/bin/libprotobuf.dll;
debug;C:/vcpkg/vcpkg/installed/x64-windows/debug/bin/libprotobufd.dll (found version
"3.21.12")
-- Configuring done (44.2s)
-- Generating done (0.2s)
-- Build files have been written to: C:/cmake_tut/opencv_p1/build
Once the project is configured. The building and linking can be started by the following build command.
PS C:\cmake_tut\opencv_p1\build> cmake --build .
MSBuild version 17.8.0-preview-23431-02+3c910ba83 for .NET Framework

  1>Checking Build System
  Building Custom Rule C:/cmake_tut/opencv_p1/CMakeLists.txt
  main.cpp
  main.vcxproj -> C:\cmake_tut\opencv_p1\build\Debug\main.exe
  Building Custom Rule C:/cmake_tut/opencv_p1/CMakeLists.txt

Additional resources VCPKG for FFMPEG and OpenCV with dnn-cuda 

VVPkg and CMake guide to install and use OpenCV libraries with VCPKG, FFMPEG, dnn-cuda, and cudnn can be found on my blog here.
In the referenced article described options for dnn-cuda with a manual prerequisite to install cudnn from Nvidia for the following command Opencv4[gstreamer,ffmpeg,dnn-cuda]:x64-window.

Conclusion using VCPKG and Cmake in the OpenCV project

Cmake and VCPKG is the perfect combination to start coding your Opencv project. it is easy, and fast and you can code in the editor of your choice. I used VS code and kept my Visual Studio 2022 just to compile the project.
The Dlls needed to run the project are automatically managed, which means copied to the folder where the executable file is located as you can see in the following picture. the executable file is located. as you can see in the following picture.
 

You can start your next project much faster than building Opencv on your own. In the medium article you will find the chapters covered on this blog + a video streaming example from the Opencv app to the VLC player. All installation is done by VCPKG + GStreamer needs to be set up in environmental variables.
Next Post Previous Post
1 Comments
  • hyperboleandahalf.blogspot.com
    hyperboleandahalf.blogspot.com April 17, 2024 at 4:58 AM

    Our best japanese food new york don't just serve food; we serve an experience that transports guests to the bustling streets of Tokyo or the serene countryside of Kyoto. We take pride in preserving the rich culinary traditions of Japan, offering a menu that celebrates the authentic flavors and techniques of Japanese cuisine.

Add Comment
comment url