Writing Our Own Find-Modules

Let's learn how to write and find custom package in CMake.

We'll cover the following

On a rare occasion, the library we want to use in our project doesn't provide a config-file or a PkgConfig file, and there's no find-module readily available in CMake already. We can then write a custom find-module for that library and ship it with our project. This situation is not ideal, but in the interest of taking care of the users of our project, it has to be done.

Example

Since we have already become familiar with libpqxx in the previous section, let's write a nice find-module for it. We start by writing in a new FindPQXX.cmake file, which we'll store in the cmake/module directory of our project source tree. We need to make sure that the find-module gets discovered by the CMake when find_package() is called, so we'll add this path to the CMAKE_MODULE_PATH variable in our CMakeLists.txt with list(APPEND). The whole list file should look like this:

Get hands-on with 1200+ tech skills courses.