How To: Statically Link C++ Libraries with vcpkg + Visual Studio 2019

David Hill
Level Up Coding
Published in
6 min readApr 3, 2020

--

Dealing with C++ libraries and statically linking them to your project can be an absolute pain to do manually. Luckily there are package managers like vcpkg that can make downloading and managing C++ dependencies a lot easier. Unfortunately, (or fortunately depending on your view) vcpkg works uses dynamic linking out of the box but static linking your libraries requires some leg work. I went through a lot of pain to figure out how to statically link packages with vcpkg and Visual Studio so I am writing this tutorial so you don’t have to go through the pain that I did. In this tutorial I will show you how to set up vcpkg and integrate it with your Visual Studio C++ project. Finally, I will walk through how to set up Visual Studio to use the static libraries downloaded by vcpkg so lets get started!

0. VCPKG Install and Setup

To start, make sure you have git and Visual Studio 2019 installed on your machine. Here you can download git and Visual Studio.

Next we are going to download vcpkg. To do this you will need to visit the vcpkg GitHub repository here. Once, there you will click on the green “Clone or download” button and copy the link that appears.

GitHub vcpkg

Now go back to your desktop and type:

Windows Key + R

and in the dialog box enter:

cmd

The Windows command prompt will load and you should see something like this:

Windows command prompt

In the command prompt enter git clone and the GitHub link you copied before and hit enter:

> git clone https://github.com/microsoft/vcpkg.git

Your output should look like this:

Result after cloning vcpkg

You have now successfully downloaded vcpkg! So now lets install it and download some packages!

In the command prompt go into the vcpkg directory:

> cd vcpkg

Then to install enter:

> bootstrap-vcpkg.bat

Your output should look like this:

Install vcpkg

Next we want to integrate vcpkg with our installation of Visual Studio so to do that all you have to do is enter:

> vcpkg integrate install

Your output will look like this:

Integrate vcpkg with Visual Studio

Don’t mind that I am now in a folder called “Projects” as I had a previous installation of vcpkg.

1. Downloading Dependencies

You’ve done great so far so lets install some packages!

To search packages to download enter:

> vcpkg search <package name>

For example lets search for the spdlog library:

vcpkg search spdlog

We found the spdlog package! Now lets download it.

There are two ways to go about this. If you want to download the package and use it with dynamic linking all you need to do is enter: vcpkg install spdlog. But we don’t want to do that because we want to download the static libraries for our desired platform. In this example we will download spdlog for the x64 Windows platform.

To do this enter:

> vcpkg install spdlog:x64-windows-static

You should see the following output:

Installing spdlog static libraries

We have now downloaded our library so lets configure Visual Studio to use it!

2. Configuring Visual Studio

First, open Visual Studio 2019 and click on “Create a new project”.

Visual Studio 2019

Select “Console App”:

Create a new project

Enter the name of your project and hit the “Create” button.

Configure your project

Now here is where the fun begins.

Since we are using the x64 platform the first thing we want to do is change our project from the default x86 to x64.

To do this, change the drop down at the top of the screen from x86 to x64 so it looks like this:

Debug x64

Next, go over to the Solution Explorer and click on the ‘Folder View” Icon and select the option “Folder View”

Folder View Icon

You should see your projects solution files:

Project Folder View

Click on the .vcxproj file in the list. You will see an xml document show up. Scroll down a bit until you see the <PropertyGroup Label=”Globals”> tag.

Property Group Globals

At the very bottom add the following line:

<VcpkgTriplet Condition="'$(Platform)'=='x64'">x64-windows-static</VcpkgTriplet>

This will look different depending on the platform you decide to use for example if you are using win32 x86 application we would put:

<VcpkgTriplet Condition="'$(Platform)'=='Win32'">x86-windows-static</VcpkgTriplet>

You could use both in fact but for our example your file should look like this:

Save your changes and click on the “Folder View” icon again and select <YourProject>.sln in my case its DemoStaticLinking.sln. This is what you will see as a result:

Result after clicking “Folder View” icon

You need to load your project again so left click on the first item select “Load All Projects” and click yes until you get back to your main project.

The last step is to change our C runtime environment to one that supports static linking. Without this, you will encounter linker errors when trying to use your libraries.

Left click on your project solution and select properties at the bottom or hit

Alt + Enter

Once the dialog box pops up you want to go to C/C++ > Code Generation:

Properties

If you see “Configuration: Release” at the top change the Runtime Library property to “Multi-threaded (/MT)”:

Hit apply. Then change “Configuration: Release” to “Configuration: Debug” and change the C Runtime Library to “Multi-threaded Debug (/MTd)”:

At this point we are done! We can now use our vcpkg static libraries and compile them!

In your main cpp file (mine is DemoStaticLinking.cpp) add the following:

Click the green triangle at the top labeled “Local Windows Debugger” to run the project and you should get this output:

Final Output

You did it! You successfully integrated vcpkg with Visual Studio and configured your Visual Studio project to link static libraries! Now it should be easier than ever to install C++ libraries and statically link them to your projects. I hope this tutorial was helpful in streamlining your C++ development workflow. Happy coding! :)

--

--