cmake Search and use installed packages, libraries and programs Use pkg_search_module and pkg_check_modules


Example

On Unix-like operating systems, it is possible to use the pkg-config program to find and configure packages that provides a <package>.pc file.

In order to use pkg-config, it is necessary to call include(FindPkgConfig) in a CMakeLists.txt. Then, there are 2 possible functions:

  • pkg_search_module, which checks for the package and uses the first available.
  • pkg_check_modules, which check for all the corresponding packages.

Here's a basic CMakeLists.txt that uses pkg-config to find SDL2 with version above or equal to 2.0.1:

cmake_minimum_required(2.8 FATAL_ERROR)
project("SDL2Test")

include(FindPkgConfig)
pkg_search_module(SDL2 REQUIRED sdl2>=2.0.1)

include_directories(${SDL2_INCLUDE_DIRS})
add_executable(${PROJECT_NAME} main.c)
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES})