How to do it

Let us start with the same source code as for the previous recipe. We want to be able to toggle between two behaviors:

  1. Build Message.hpp and Message.cpp into a library, static or shared, and then link the resulting library into the hello-world executable.
  2. Build Message.hpp, Message.cpp, and hello-world.cpp into a single executable, without producing the library.

Let us construct CMakeLists.txt to achieve this:

  1. We start out by defining the minimum CMake version, project name, and supported language:
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)project(recipe-04 LANGUAGES CXX)
  1. We introduce a new variable, USE_LIBRARY. This is a logical variable and its value will be set to OFF. We also print its value for the user:
set(USE_LIBRARY ...

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.