Easy setup: vcpkg, CMake and Visual Studio Code

Computer vision researchers and application developers who work in Linux environment may take the C++ package management for granted but Windows users who need to use different libraries which is often in the case of computer vision application development, surely knows that this is a daunting task.

VCPKG

vcpkg is a C++ package manager from Windows which offers 1500+ open source libraries and a fairly simple method of installation, somewhat like Linux.

VISUAL STUDIO CODE

Visual Studio Code Editor or (VS Code) is becoming very popular amongst developers for its light-weight, clean and friendly UI, support for various programming languages and a rich ecosystem of extensions. For instance, one can easily develop C++ and Python applications in Raspberry Pi using VS code. And last but not the least its free!

CMAKE

CMake is a cross-platform build system generator which is quite popular and with modern CMake it is even more easier to handle bigger projects.

We can either use Visual Studio or MingGW build tools depending on your needs. It is important to keep in mind that Visual Studio Community edition is only for personal use.

Now, let us get started with installation of each item. A step maybe skipped if the software package is already installed.

Installation:

  1. vcpkg – default installation and add to PATH variable
  2. CMake – default installation and add to PATH variable
  3. Visual Studio – (Optional)
  4. Visual Studio Code Editor – default installation.
  5. MingGW – using Msys2, a nice tutorial is here

Installation of packages using vcpkg:

Open Windows Command prompt and navigate to vcpkg root directory for example: C:\src\vcpkg , inside which the vcpkg.exe is located. Please note that here we will be installing the packages for only x64 architecture to save memory. Then run the following command to install opencv:

vcpkg install opencv:x64-windows

Setup VS code:

Open VS code and go to extensions. Then add the following extensions:

  • CMake
  • CMake Tools
  • C/C++ for Visual Studio Code
  • Create a project folder and open it in VS code
  • Create the CMakeLists.txt file inside the project folder:
    • Open Command Palette using Ctrl+Shift+P and type “CMake: Quick Start” and press enter
    • Scan for kits
    • Select either MVSC or GCC, with x64 configuration
    • Skip the search for existing CMakeLists.txt
    • Enter project name, example: “myproject”
    • Enter target type as “executable”, since we will be building executable and linking a library in this demo
    • On completion of the above texts a template CMakeLists.txt will be generated along with a template main.cpp file in the project folder.
    • An image file, for instance here “lena.png” should be present in the same path as that of the executable.

Code in main.cpp:

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int, char**)
{
    string image_path = "lena.png";
    Mat img = imread(image_path, IMREAD_COLOR);
    if(img.empty())
    {
        cout << "Could not read the image: " << image_path << endl;
        return 1;
    }
    imshow("Display window", img);
    int k = waitKey(0); // Wait for a keystroke in the window
    destroyAllWindows();
    return 0;
}

Code in CMakeLists.txt:

cmake_minimum_required(VERSION 3.18)

include("C:\\src\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake\\")
set(CMAKE_TOOLCHAIN_FILE "C:\\src\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake")
set(VCPKG_TARGET_TRIPLET "x64-windows")

project(myproj VERSION 0.1.0)

include(CTest)
enable_testing()

find_package(OpenCV REQUIRED)

add_executable(myproj main.cpp)
target_link_libraries(myproj PRIVATE $(OpenCV_LIBS))

Build and Run:

Thereafter build the project with the following steps also marked in the figure beneath:

  1. Set CMake configuration to “Debug” or “Release”
  2. Set the build tool, here for example VS2022-amd64
  3. Build
  4. Run

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *