Discovering Legacy Packages With FindPkgConfig
Let's learn how to find and use legacy packages with FindPkgConfig.
We'll cover the following
The problem of managing dependencies and discovering all the compile flags that they require is as old as C++ libraries themselves. There are many tools to deal with it, ranging from very small and minimal mechanisms to very versatile solutions offered as parts of buildsystems and IDEs. One of the (once very popular) tools is called PkgConfig
. It is often available on Unix-like systems (although it works on macOS and Windows too).
Discovering legacy package
pkg-config
is slowly being phased out by other, more modern solutions. A question arises here: should we invest our time in supporting it? The answer is as usual: it depends:
If a library is really popular, it might already have its find-module in CMake; in that case, we probably won't need it.
If there's no find-module (or it doesn't work for our library) and a
PkgConfig
.pc
file is all that library provides, just use what's readily available.
Many (if not most) libraries have embraced CMake and provide a package config-file in current versions. If we're not publishing our solution and we control the environment, use find_package()
and don't worry about legacy versions.
Sadly, not all environments can be quickly updated to the latest versions of a library. A lot of companies are still using legacy systems in production, which are no longer getting the latest packages. In that case, users might be stuck with an older (but hopefully compatible) version. And very often, it will provide a .pc
file.
Additionally, efforts to support the older PkgConfig
format might be worthwhile if it means that our project will work out of the box for most users.
In any case, start by using find_package()
, and if <PKG_NAME>_FOUND
is false, fall back on PkgConfig
. This way, we cover a scenario where an environment gets upgraded, and we can just use the main method without changing the code.
The concept of this helper tool is quite simple—the author of the library provides a small .pc
file containing details necessary for compilation and linking, such as this one:
Get hands-on with 1400+ tech skills courses.