How to do it

These are the steps to follow in our CMakeLists.txt:

  1. The first block contains the minimum CMake version, project name, and required language:
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)project(recipe-02 LANGUAGES C)
  1. In this recipe we enforce the use of the C99 standard for C. This is not strictly required for linking with Python, but is something you might want to have in place:
set(CMAKE_C_STANDARD 99)set(CMAKE_C_EXTENSIONS OFF)set(CMAKE_C_STANDARD_REQUIRED ON)
  1. Find the Python interpreter. This is now a required dependency:
find_package(PythonInterp REQUIRED)
  1. Find the Python header and library. The appropriate module is called FindPythonLibs.cmake:
find_package(PythonLibs ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR} ...

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.