Raymond Raymond

Microsoft vcpkg C++ Library Manager

event 2021-01-17 visibility 2,971 comment 0 insights toc
more_vert
insights Stats

Microsoft vcpkg is a C++ library managers for Windows, macOS and Linux. For .NET, nuget is the commonly used package manager; for Java,  Maven central and many other repositories are used by Maven or other build tools; for JavaScript, npm is a commonly used package manager and it is also the default package manager for JavaScript runtime environment Node.js. Microsoft vcpkg serves as similar purpose for C++ developers. 

Install vcpkg on Windows

Before we can install vcpkg, make sure the following criteria are met:

  • Git - for cloning repository from GitHub
  • Windows 7 or newer
  • Visual Studio 2015 Update 3 or greater with the English language pack

Follow these steps to install vcpkg.

  1. Create installation folder. I recommend creating the folder in C drive to avoid path related issues.
    C:\>mkdir dev
    C:\> cd dev
  2. Clone the repository using the following command:
    git clone https://github.com/microsoft/vcpkg
  3. Run the following command to install vcpkg:
    .\vcpkg\bootstrap-vcpkg.bat
  4. Wait until the command is completed successfully. 
    20210116234108-image.png

Now we can use vcpkg to install C++ libraries.

Find libraries

Subcommand search is used to search libraries. The syntax looks like the following:

.\vcpkg\vcpkg search [search term]

For example, the following command searches for library openssl:

.\vcpkg\vcpkg search openssl
...
openssl              1.1.1i           OpenSSL is an open source project that provides a robust, commercial-grade, an...
openssl-unix         1.1.1h#1         Deprecated OpenSSL port
openssl-uwp          1.1.1h#1         Deprecated OpenSSL port
openssl-windows      1.1.1h#1         Deprecated OpenSSL port
...

The results include a number of openssl related libraries. 

Integrate vcpkg with Visual Studio

Run the following command to integrate vcpkg with Visual Studio:

.\vcpkg\vcpkg integrate install

The output looks like the following:

C:\dev>.\vcpkg\vcpkg integrate install
Applied user-wide integration for this vcpkg root.

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

CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=C:/dev/vcpkg/scripts/buildsystems/vcpkg.cmake"

As mentioned in the output, you can include any installed libraries in your MSBuild C++ projects. 

Use vcpkg in CMake

To use vcpkg with CMake outside of an IDE, you can use the toolchain file for CMake:

cmake -B [build directory] -S . -DCMAKE_TOOLCHAIN_FILE=[path to vcpkg]/scripts/buildsystems/vcpkg.cmake

For this article, the path is:

cmake -B [build directory] -S . -DCMAKE_TOOLCHAIN_FILE=C:/dev/vcpkg/scripts/buildsystems/vcpkg.cmake

Use CMake with Visual Studio

Open the CMake Settings Editor, and under CMake toolchain file, add the path to the vcpkg toolchain file:

C:/dev/vcpkg/scripts/buildsystems/vcpkg.cmake

Example - openssl

Now let's implement this example that installs openssl: CMake Build Error - Could not Find OpenSSL on Windows 10.

Follow these steps:

  1. Run the following command to install package openssl:
    C:\dev>.\vcpkg\vcpkg install openssl:x64-windows

    Alternatively, you can also use the following command:

    C:\dev>.\vcpkg\vcpkg install openssl --triplet x64-windows

    I am using triplet x64-windows to match my project requirements. By default x86 triplet will be used.

    The following text will be printed out if in your system CMake version is lower than 3.19.2. I would recommending upgrade CMake versions. 

    A suitable version of cmake was not found (required v3.19.2). Downloading portable cmake v3.19.2...
    Downloading cmake...
      https://github.com/Kitware/CMake/releases/download/v3.19.2/cmake-3.19.2-win32-x86.zip -> C:\dev\vcpkg\downloads\cmake-3.19.2-win32-x86.zip

    For the first time, many other dependencies will also be downloaded. It will takes a while to download for the first time. 
    2021011710309-image.png

  2. Test openssl by running the following command:
    C:\dev>openssl version
    OpenSSL 1.1.1g  21 Apr 2020
  3. Create a project folder named C:\hdp\cmaketest.
  4. Create file CMakeLists.txt in the project folder with the following content:
    cmake_minimum_required(VERSION 3.10)
    
    # set the project name
    project(Test)
    
    # Find package
    find_package(OpenSSL REQUIRED)
  5. Change directory to C:\hdp\cmaketest.
    cd  /D C:\hdp\cmaketest
  6. Run CMake command:
    cmake . -DOPENSSL_ROOT_DIR="C:/dev/vcpkg/installed/x64-windows"

    The output looks like the following:
    2021011712148-image.png

  7. Congratulations, openssl dependency is met. 
More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts