A C/C++ package manager
Nicely Packaged
![© Lead Image © Hung Ling Tie, 123RF.com © Lead Image © Hung Ling Tie, 123RF.com](/var/linux_magazin/storage/images/issues/2024/280/vcpkg/hung_ling_tie_123rf-presents.png/834784-1-eng-US/Hung_Ling_Tie_123RF-presents.png_medium.png)
© Lead Image © Hung Ling Tie, 123RF.com
If you use a state-of-the-art language such as C/C++, you need a package manager in your toolbox for managing libraries and their dependencies. Microsoft's vcpkg package manager offers an easy-to-use, platform-independent solution.
Today, hardly any developer writes the complete source code for an application. Preconfigured libraries for logging, database access, or a geometric modeling kernel speed up solution development. Using libraries lets compilers and linkers access a library's header files and DLLs, whether the libraries are used directly or referenced. Tools such as Maven for Java or NuGet for C# elegantly solve this task. At build time, they fetch the specified libraries from the Internet and resolve their transitive dependencies.
This type of package manager uses two different file types: the package description and the library file. In addition to the library's name and version, the package description contains a list of the other required libraries. Depending on the language used, the libraries themselves are available, say, as Java archives (JAR), C# DLLs, or JavaScript files. Both file types are typically provided on central servers such as Maven Central [1] or NuGet.org [2] and can be downloaded from there.
Your application includes a package description listing the required libraries. The package manager then downloads the requested package descriptions and library files from the central server before compiling. The package manager also takes care of resolving transitive dependencies before the build process starts using the library file, which is now available locally.
Doing Its Own Thing
Until the advent of solutions such as vcpkg [3] and Conan [4], libraries and their dependencies were typically managed manually in the C++ environment. Vcpkg, an open source solution developed at Microsoft, is available on GitHub [5] under the MIT License; the alternative package manager, Conan, developed by JFrog, is also available under the MIT License on GitHub [6]. Both can be used as standalone tools, but they can also be easily integrated into popular build systems such as CMake or MSBuild.
While Conan relies on precompiled libraries, vcpkg builds the libraries from the source code on the developer's machine. The large number of platforms and compilers makes it difficult to keep fully compiled libraries for all combinations available at a central location. Local builds sidestep this difficulty right from the start. Vcpkg also removes the need to provide a server that is available at all times to download the package description and libraries and to secure it against the risk of spyware downloads.
Figure 1 shows vcpkg's process. When launched, the package manager reads the project manifest, determines the project's dependencies, and searches for them in the cache. If a dependency is not available at this point, it reads the dependency's manifest from the registry and downloads the project sources. The package manager then builds the library and saves it in the cache. Once this has been done for all direct and indirect dependencies, the project itself can be built.
There are two options for the cache. Classic mode uses a central storage location for all projects. In manifest mode, there is a separate storage location for each project. To avoid conflicts between different projects' dependencies, it is generally advisable to use manifest mode.
The manifest for a library consists of at least one file named vcpkg.json
. Listing 1 shows an example of the fmt
library [7]. In addition to the library name, this file primarily contains a list of the dependencies. Both the library's validity and the dependencies can be restricted to specific platforms if required. Where the use case is more complex (e.g., if other repositories need to resolve dependencies), you can enforce additional information in the optional vcpkg-configuration.json
file. Listing 2 show an example CMake file.
Listing 1
Manifest File for fmt
{ "name": "fmt", "version": "9.1.0", "port-version": 1, "description": "Formatting library for C++. ... ", "homepage": "https://GitHub.com/fmtlib/fmt", "license": "MIT", "dependencies": [ { "name": "vcpkg-cmake", "host": true }, { "name": "vcpkg-cmake-config", "host": true } ] }
Listing 2
Example CMake File
cmake_minimum_required(VERSION 3.25) set(VCPKG_EXAMPLE_VERSION 0.1.0) project(vcpkg-example VERSION ${VCPKG_EXAMPLE_VERSION} LANGUAGES CXX) # Dependencies find_package(fmt CONFIG REQUIRED) find_package(Boost REQUIRED COMPONENTS program_options) # Set libs to link against list(APPEND VCPKG_EXAMPLE_LIBS fmt::fmt Boost::boost Boost::program_options) # Build add_executable(${PROJECT_NAME} src/main.cc) target_link_libraries(${PROJECT_NAME} PRIVATE ${VCPKG_EXAMPLE_LIBS})
Step by Step
Using the package manager for your programs is relatively simple. A complete example can be found on the vcpkg [8] GitHub page. Listing 3 shows the steps for compiling on the command line. The commands from lines 1 and 2 load vcpkg and the sample program off GitHub. To initialize the package manager before using it for the first time, change to its directory and call the bootstrap-vcpkg.sh
script (lines 3 and 4).
Listing 3
Compiling the Example Program
01 $ git clone https://github.com/skfcz/vcpkgBeispiel.git 02 $ git clone https://github.com/Microsoft/vcpkg.git 03 $ cd vcpkgExample 04 $ sh vcpkg/bootstrap-vcpkg.sh 05 [...] 06 $ cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=vcpkg/scripts/buildsystems/vcpkg.cmake 07 $ cmake --build build/ build/vcpkg-example --option World
Next, call CMake in the example program's folder so that it uses the vcpkg toolchain to load and compile the required libraries. As shown in line 6, you do need to set the appropriate CMake option. The same principle applies if you use an IDE. Figure 2 shows the entry for CLion.
Then you just have to wait for vcpkg to resolve the dependencies requested by the sample program in the registry, download their sources, and compile them. If the dependencies are huge, or complex, this can take some time, depending on your system's compute performance.
As a reward for all of this work, the requested libraries' header files and the compiled binaries are now safely stored in the cache directory. The required libraries are listed in the program's package description. The example shown in Listing 4 is a library for handling command-line options and another library for neat string formatting.
Listing 4
Example Package Description
{ "name": "vcpkg-example", "version": "0.1.0", "dependencies": [ { "name": "boost-program-options" }, { "name": "fmt" } ] }
Package Collection
On the vcpkg website [3], a search function in Packages will help you find the correct names for the package description entries. You should be able to find a solution for most tasks given that there are 2,000 libraries. Alternatively, you can search for the libraries by calling vcpkg search
at the command line.
After compiling the required libraries, the next step is to compile your program with CMake and link to the newly created libraries (Listing 3, line 7). This does not require any changes to the CMake file (Listing 2) compared to a version for locally installed libraries – provided that the libraries can be found using CMake.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.