Microsoft vcpkg C++ Library Manager
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.
- Create installation folder. I recommend creating the folder in C drive to avoid path related issues.
C:\>mkdir dev C:\> cd dev
- Clone the repository using the following command:
git clone https://github.com/microsoft/vcpkg
- Run the following command to install vcpkg:
- Wait until the command is completed successfully.
Now we can use vcpkg to install C++ 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:
Example - openssl
Now let's implement this example that installs openssl: CMake Build Error - Could not Find OpenSSL on Windows 10.
Follow these steps:
- 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...
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.
- Test openssl by running the following command:
C:\dev>openssl version OpenSSL 1.1.1g 21 Apr 2020
- Create a project folder named C:\hdp\cmaketest.
- 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)
- Change directory to C:\hdp\cmaketest.
cd /D C:\hdp\cmaketest
- Run CMake command:
cmake . -DOPENSSL_ROOT_DIR="C:/dev/vcpkg/installed/x64-windows"
The output looks like the following:
- Congratulations, openssl dependency is met.