When working with C++, we’re not restricted to just working with the standard library. There is a huge and diverse ecosystem of other libraries we can use, covering a large range of uses.
Rather than reinventing the wheel in every project, we can import libraries created by other developers that can meet our needs.
In upcoming lessons, we’re going to begin relying more and more on these external libraries.
However, manually downloading and managing packages is a cumbersome process. A range of options have been created that help us manage our dependencies more effectively.
The most popular options are currently vcpkg and Conan.
For this course, we’ll use vcpkg, and in this lesson, we’ll walk through the installation process and some common issues that can occur in Windows. but feel free to use any option
We need to compile vcpkg from the source code, but a simple script is provided that will do this for us with a single command.
A common installation and setup for vcpkg involves four steps:
We walk through these four steps below.
Recent versions of Visual Studio allow vcpkg to be installed as an individual component, alongside Visual Studio. This approach can cause issues, so we should use the manual installation process instead.
The version of vcpkg that is integrated with Visual Studio only supports "manifest mode", which is a more complex form of dependency management. We cover manifest mode in a later chapter when we introduce build automation.
A zip containing the source code we need is available from the releases page on GitHub.
We should download the source code from the Assets section of the latest release:
Once acquired, we should unzip this folder to a location on our hard drive. We will be accessing it frequently, so ensure it is in a location we’ll easily be able to find later.
Git users can quickly clone the latest code, rather than downloading a zip file:
git clone https://github.com/Microsoft/vcpkg.git
To compile vcpkg and generate the executable file, we can run the bootstrap-vcpkg.bat
in the folder where we unzipped everything.
Once complete, the script should have created an executable file - vcpkg.exe - in the same directory.
To easily use the packages we download, we need to integrate vcpkg with our C++ build tools. By default, this is MSBuild on Windows.
vcpkg also has a script for this, as part of the executable we generated in the previous step
vcpkg.exe is a command line tool. To use it, we need to execute it from a terminal, which allows us to pass additional arguments.
On Windows, we can open a Power Shell terminal window in our vcpkg directory using Shift + Right Click > Open PowerShell Window here
From PowerShell, we can run the following command to integrate vcpkg with MSBuild:
.\vcpkg integrate install
This should generate a success message, indicating C++ projects can now #include installed libraries.
If this script generates an error stating vcpkg is not recognized, we need to ensure we ran the bootstrap-vcpkg.bat file from step 2, and that we’re opening our PowerShell window in the same location as the vcpkg.exe file that was generated.
There are many possible environments we can build C++ applications for. For example, these can include PCs running Windows, phones running iOS, smartwatches, etc. A triplet is a simple string that specifies what the target environment is. For example, x64-windows
specifies our target is 64-bit Windows devices.
When we download a package through vcpkg, we need to specify which target we want. We can do that every time we install a package, but it’s usually easier to specify a default.
We do that by creating a VCPKG_DEFAULT_TRIPLET
as an environment variable.
We can do that through Control Panel > System Properties > Advanced > Environment Variables, and click "New" within the User Variables section.
Alternatively, we can run this PowerShell command:
[System.Environment]::SetEnvironmentVariable(
'VCPKG_DEFAULT_TRIPLET','x64-windows', 'User'
)
We can install packages through vcpkg by using the vcpkg install
command, followed by the names of the packages we want to install.
Let's now install the SDL libraries with vcpkg. As before, let’s open a PowerShell terminal in our vcpkg installation directory, and run this command:
.\vcpkg install sdl2 sdl2-image sdl2-ttf
If successful, we should now be able to immediately #include
this library in our Visual Studio projects, and ensure they compile successfully:
#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
int main(){
SDL_Init(SDL_INIT_VIDEO);
IMG_Init(IMG_INIT_PNG);
TTF_Init();
}
If the #include
directive isn’t working following the above steps, the problem is likely to be one of two possibilities:
integrate
script didn’t runEnsure step 2 from the installation instructions above is completed successfully.
We need to ensure the platform we’re building for in Visual Studio matches the target of the package we downloaded. We can see our Visual Studio target (eg x64) from the dropdown near the Run button, or Build Menu > Configuration Manager
In the above example, we’re building for x64, or the x64-windows
triplet.
We can see what vcpkg packages we have downloaded by running .\vcpkg.exe list
from the vcpkg directory. For example, if our Visual Studio build configuration is targeting x64, we should ensure that the list includes the x64-windows
version of our package:
.\vcpkg.exe list
If it’s missing, we can specify the desired triplet as an additional argument to the vcpkg install
command:
.\vcpkg install sdl2 --triplet x64-windows
Alternatively, we can set the default triplet by following step 4 of the installation instructions above. The default triplet will be used when we do not explicitly specify one as part of the install
command.
In this lesson, we've navigated through the steps of installing vcpkg on Windows, allowing us to manage external C++ libraries. The key takeaways include:
An introduction to C++ package managers, and a step-by-step guide to installing vcpkg on Windows and Visual Studio.
Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games