How to do it

We will construct the CMakeLists.txt step by step and show how to require a certain standard (in this case C++14):

  1. We state the minimum required CMake version, project name, and language:
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)project(recipe-09 LANGUAGES CXX)
  1. We request all library symbols to be exported on Windows:
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
  1. We need to add a target for the library. This will compile the sources into a shared library:
add_library(animals  SHARED    Animal.cpp    Animal.hpp    Cat.cpp    Cat.hpp    Dog.cpp    Dog.hpp    Factory.hpp  )
  1. We now set the CXX_STANDARD, CXX_EXTENSIONS, and CXX_STANDARD_REQUIRED properties for the target. We also set the POSITION_INDEPENDENT_CODE property, to avoid issues when ...

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.