How to do it

This is how we can use the set_compiler_flag function in our CMakeLists.txt:

  1. In the preamble, we define the minimum CMake version, project name, and supported languages (in this case, C and C++):
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)  project(recipe-03 LANGUAGES C CXX)
  1. Then, we include set_compiler_flag.cmake, in this case explicitly:
include(set_compiler_flag.cmake)
  1. Then, we try a list of C flags:
set_compiler_flag(  working_compile_flag C REQUIRED  "-foo"             # this should fail  "-wrong"           # this should fail  "-wrong"           # this should fail  "-Wall"            # this should work with GNU  "-warn all"        # this should work with Intel  "-Minform=inform"  # this should work with PGI  "-nope"            # this should fail  )message(STATUS "working C compile ...

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.