CMake Cookbook
上QQ阅读APP看书,第一时间看更新

Detecting external libraries: I. Using pkg-config

The code for this recipe is available at https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-03/recipe-09 and has a C example. The recipe is valid with CMake version 3.6 (and higher) and has been tested on GNU/Linux, macOS, and Windows (using MSYS Makefiles). In  https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-03/recipe-09, we also provide an example compatible with CMake 3.5.

We have so far discussed two ways of detecting external dependencies:

  • Using find-modules shipped with CMake. This is generally reliable and well tested. However, not all packages have a find-module in the official release of CMake.
  • Using <package>Config.cmake<package>ConfigVersion.cmake, and <package>Targets.cmake files provided by the package vendor and installed alongside the package itself in standard locations.

What if a certain dependency provides neither a find-module nor vendor-packaged CMake files? In this case, we are left with two options:

  • Rely on the pkg-config utility to discover packages on the system. This relies on the package vendors distributing metadata about their packages in .pc configuration files.
  • Write our own find-package module for the dependency.

In this recipe, we will show how to leverage pkg-config from within CMake to locate the ZeroMQ messaging library. The next recipe, Detecting external libraries: II. Writing a find-module, will show how to write your own basic find-module for ZeroMQ.