How it works

The pattern that we have used here is:

  1. Define a function or macro and place it in a module
  2. Include the module
  3. Call the function or macro

From the output, we can see that the code checks each flag in the list and as soon as the check is successful, it prints the successful compile flag. Let us look inside the set_compiler_flag.cmake module. This module, in turn, includes three modules:

include(CheckCCompilerFlag)include(CheckCXXCompilerFlag)include(CheckFortranCompilerFlag)

These are standard CMake modules and CMake will locate them in ${CMAKE_MODULE_PATH}. These modules provide the check_c_compiler_flag, check_cxx_compiler_flag, and check_fortran_compiler_flag macros, respectively. Then comes the function definition:

function(set_compiler_flag ...

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.