How to do it

We will build up the CMakeLists.txt file step by step:

  1. We start out by defining the minimum CMake version and project name. Note that for this example we will not need any language support:
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)project(recipe-01 LANGUAGES NONE)
  1. Then, we use the find_package command to find the Python interpreter:
find_package(PythonInterp REQUIRED)
  1. Then, we execute a Python command and capture its output and return value:
execute_process(  COMMAND    ${PYTHON_EXECUTABLE} "-c" "print('Hello, world!')"  RESULT_VARIABLE _status  OUTPUT_VARIABLE _hello_world  ERROR_QUIET  OUTPUT_STRIP_TRAILING_WHITESPACE  )
  1. Finally, we print the return value and the output of the Python command:
message(STATUS "RESULT_VARIABLE ...

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.