vcpkg

It’s best if you can avoid re-inventing the wheel. If someone has already written the code you need then it can save a lot of time. Although you should probably make sure about licences before incorporating it. That could be copy-pasting something off a web page but, especially for bigger bits of code, far better as a library. However, as you get more libraries and they themselves need other libraries it can get complicated. vcpkg lets you outsource a lot of that complexity.

The old way

Let’s say you want to use the C++ fmt library. There are a variety of ways to get the library. Perhaps installing it on a Linux system using APT:

apt install libfmt-dev

But installing on the system requires documentation or a setup script. It something that’s easy to miss when setting up a project. Maybe make use of the CMake build system to fetch it for you:

include(FetchContent)

FetchContent_Declare(
    fmt
    GIT_REPOSITORY https://github.com/fmtlib/fmt
    GIT_TAG e69e5f977d458f2650bb346dadf2ad30c5320281) # 10.2.1
FetchContent_MakeAvailable(fmt)

This is pretty good. You just need need to run cmake and it will download and build the library. However it’s going to do that every time you run cmake which can be slow. This is also a simple setup with one library and no dependencies.

A new way

What you could do instead is use vcpkg. It’s most useful when you have a bunch of libraries as you will need to do some setup. However after that you can create a vcpkg.json file:

{
    "dependencies": [
        "fmt"
    ]
}

If you want more packages just add them to the list. There’s an extensive package directory. They will be downloaded and compiled before you do your build. All the artefacts will be cached so it only has to happen when you change your build setup.

If the package you want has it’s own dependencies you don’t have to worry about it. vcpkg will download everything that’s needed first. By default it just gets the latest available version but you can specify the package version. If the library you want isn’t available or needs customisation you can even write an overlay port to pull it in to the system yourself.

It’s not perfect. Especially if you are dealing with more advanced features like overlay triplets. The caching mechanism can feel a bit mysterious. Sometimes you change the build system and suddenly it “decides” to recompile. It has all the downloaded source in it’s cache but it will have to recompile the source. Unfortunately I’ve used some big libraries and been hit by long compile times. That’s not too bad as a one off but it kept happening. I’m going to say this is more a problem of understanding when it happens rather a bug in the system.

In the end

There are a lot of ways to consume libraries. If you only need a few it probably doesn’t matter too much. However it’s one of those things that can gradually mount up. It’s easy to find yourself using versions of libraries that are years old and it takes so long to update them all individually. This puts everything in one place and gives a framework for handling it.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *