How to do it

We wish to check whether the UUID system library on GNU/Linux can be linked against, before embarking on building our own C++ project. This can be achieved with the following series of steps:

  1. We start by declaring a mixed C and C++11 program. This is needed since the test code snippet we want to compile and run is in the C language:
cmake_minimum_required(VERSION 3.6 FATAL_ERROR)  project(recipe-08 LANGUAGES CXX C)
set(CMAKE_CXX_STANDARD 11)set(CMAKE_CXX_EXTENSIONS OFF)set(CMAKE_CXX_STANDARD_REQUIRED ON)
  1. We need to find the UUID library on our system. This is achieved by using pkg-config. We ask for the search to return a CMake imported target using the IMPORTED_TARGET argument:
find_package(PkgConfig REQUIRED QUIET)pkg_search_module(UUID ...

Get CMake Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.