Toolchain: Install and use packages with vcpkg in Visual Studio Code
vcpkg overview
vcpkg is a free and open-source C/C++ package manager maintained by Microsoft and the C++ community that runs on Windows, macOS, and Linux. It is a C++ tool at heart and is written using C++ and CMake scripts. It is designed to address the unique pain points with managing C/C++ libraries.
Ports and triplets
A vcpkg port is a versioned build recipe that produces a package. The most common type of package is a C/C++ library consisting of headers, source code, and binaries.
A triplet captures the target build environment (cpu, os, compiler, runtime, etc.) in a single, convenient name. vcpkg provides over 70 triplets by default, but you can also define your own.
To install a package on your system, vcpkg runs the port’s recipe file, which is a CMake script. The script might contain steps to download source code or to run a build on your system. During builds, vcpkg uses the information in your triplet to ensure that the produced package matches your desired configuration.
Binary caching
While vcpkg builds libraries from source whenever it’s necessary, you can back up your built packages in a binary cache. This allows other developer machines or continuous integration runs to reference these prebuilt packages without running a new build every time. vcpkg determines if a rebuild is necessary by checking if the cache already contains a valid existing package with appropriate binaries.
Manifests
You can declare your direct dependencies and add optional features or version constraints in a manifest file. Manifest files can be checked into your source control system and shared with your team.
Versioning
vcpkg has a unique way of handling package versions. Your manifest file can reference a single, baseline version set by default. The baseline gives you hassle-free, conflict-free dependency management with full reproducibility. Furthermore, you can still have more advanced control by pinning individual package versions.
Install vcpkg
Clone the vcpkg repository and
git clone https://github.com/Microsoft/vcpkg.git
After that, navigate to the vcpkg directory and execute the bootstrap script,
For windows,
cd ./vcpkg
bootstrap-vcpkg.bat
For Linux,
cd ./vcpkg
bootstrap-vcpkg.sh
Configure vcpkg
Add the environment variable ‘X_VCPKG_ASSET_SOURCES’, and set its value to ‘x-azurl,http://106.15.181.5/’.
Add dependencies
Enter our repository, and then add the following dependencies,
vcpkg new --application
vcpkg.exe add port boost rapidjson qt qt5 opencascade vtk
Appendix I: CMakeUserPresets.json
{"version": 4,"cmakeMinimumRequired": {"major": 3,"minor": 20,"patch": 0},"configurePresets": [{"name": "vs2022","hidden": false,"displayName": "Visual Studio 17 2022","description": "This build is using Visual Studio 17 2022 generator","generator": "Visual Studio 17 2022","toolset": "host=x64","architecture": "x64","cacheVariables": {"CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"},"environment": {"VCPKG_ROOT": "D:/YouQuan/CaeFrameworks/vcpkg"},"condition": {"type": "equals","lhs": "${hostSystemName}","rhs": "Windows"}},{"name": "vs2019","hidden": false,"displayName": "Visual Studio 16 2019","description": "This build is using Visual Studio 16 2019 generator","generator": "Visual Studio 16 2019","toolset": "host=x64","architecture": "x64","cacheVariables": {"CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"},"environment": {"VCPKG_ROOT": "D:/YouQuan/CaeFrameworks/vcpkg"},"condition": {"type": "equals","lhs": "${hostSystemName}","rhs": "Windows"}},{"name": "linux-gcc","hidden": false,"displayName": "linux-gcc","description": "Default build using Unix Makefiles generator","generator": "Unix Makefiles","cacheVariables": {"CMAKE_TOOLCHAIN_FILE": "${sourceParentDir}/vcpkg/scripts/buildsystems/vcpkg.cmake"}}],"buildPresets": [{"name": "vs2022","displayName": "Visual Studio 17 2022","configurePreset": "vs2022"},{"name": "vs2019","displayName": "Visual Studio 16 2019","configurePreset": "vs2019"},{"name": "linux-gcc","displayName": "linux-gcc","configurePreset": "linux-gcc"}],"testPresets": [{"name": "vs2022","displayName": "Visual Studio 17 2022","configurePreset": "vs2022"},{"name": "vs2019","displayName": "Visual Studio 16 2019","configurePreset": "vs2019"},{"name": "linux-gcc","displayName": "linux-gcc","configurePreset": "linux-gcc"}]
}
References
- vcpkg
- Tutorial: Install and use packages with CMake in Visual Studio Code