CMake Module template to find package

Jacob su
1 min readMay 30, 2020

--

This post would introduce a CMake module that used to find the target library dependency, especially in desktop env development.

We know there is a pkg-config that used to find the library dependency in the Unix env. But how did we use it in CMake scripts? Let’s take GLib-2.0 for example, Here is what we can get from the command line:

pkg-config -cflags -libs glib-2.0

And following is the CMake module, that can use the pkg-config to find all the glib-2.0 related packages;

Although it was used to find GLib-2.0, it can be a reference that can be transformed to find other packages.

Put the above file to ${Project}/cmake directory.

Here is the CMakeLists.txt that used the above CMake module to find GLib-2.0 dependency.

Then, How did I know which libs that GLib-2.0 should be included? I included GLib to my Mac by brew, the included location is:

/usr/local/Cellar/glib/2.64.3

Then the pkgconfig directory located at

/usr/local/Cellar/glib/2.64.3/lib/pkgconfig

From there we can found how many libraries that GLib included.

--

--