qmake and vcpkg

I’ve been trying to use vcpkg lately. And if it works easily with CMake, using it with qmake is undocumented. A quick Google search gives useless results including 2 year old forum posts where the conclusion is to use CMake. However there is hope.

How vcpkg works with CMake

To understand how vcpkg can be made to work with qmake we have to understand how it works when used normally i.e. with CMake.

When you install a package with vcpkg it gets installed in the packages/<package-name>_<triplet> folder. But you do not really need to care as vcpkg provides a CMake toolchain file that will automatically detect the vcpkg installed packages.

Let’s say you want to install and use yaml-cpp, all you have to do is:

./vcpkg install yaml-cpp
cd <my_project>
mkdir build
cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=<path_to_vcpkg>/scripts/buildsystems/vcpkg.cmake
cmake . -build

Provided that in your CMakeLists.txt you have:

find_package(yaml-cpp CONFIG REQUIRED)
target_link_libraries(my_project PRIVATE yaml-cpp)

It will just work and you project will link againt the yaml-cpp library provided by vcpkg.

Making it work with qmake

Sadly, vcpkg does not provide any support for qmake, so it won’t work out of the box by simply adding a command line option.

Solution 1: pkgconfig

The easiest solution is to take advantage of pkgconfig and PKG_CONFIG_PATH. To use this you need to update your .pro with:

CONFIG += link_pkgconfig
PKGCONFIG += yaml-cpp

And then run qmake with PKG_CONFIG_PATH pointing to the vcpkg installed folder:

PKG_CONFIG_PATH=<vcpkg>/installed/x64-linux/lib/pkgconfig qmake my_project.pro

Solution 2: brute force

If you dont want to, or cannot, use pkgconfig, it is still possible to make it work by taking adavantage of the fact that vcpkg does not only put installed packages in the packages folder but also in the installed folder where we have a nice hierarchy:

installed/x64-linux/debug/lib/libyaml-cpp.a
installed/x64-linux/include/yaml-cpp/
installed/x64-linux/lib/libyaml-cpp.a

That means that you just can just add a few line to you .pro file to use vcpkg libraries.

CONFIG(debug, debug|release) {
    LIBS += -L<vcpkg>/installed/<triplet>/debug/lib
} else {
    LIBS += -L<vcpkg>/installed/<triplet>/lib
}

INCLUDEPATH += <vcpkg>/installed/<triplet>/include

And then you just need to add LIBS += -lyaml-cpp to link against the yaml-cpp library provided by vcpkg