
上QQ阅读APP看书,第一时间看更新
How to do it
We will build up the CMakeLists.txt file step by step:
- 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)
- Then, we use the find_package command to find the Python interpreter:
find_package(PythonInterp REQUIRED)
- 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
)
- Finally, we print the return value and the output of the Python command:
message(STATUS "RESULT_VARIABLE is: ${_status}")
message(STATUS "OUTPUT_VARIABLE is: ${_hello_world}")
- Now, we can examine the output of the configuration step:
$ mkdir -p build
$ cd build
$ cmake ..
-- Found PythonInterp: /usr/bin/python (found version "3.6.5")
-- RESULT_VARIABLE is: 0
-- OUTPUT_VARIABLE is: Hello, world!
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/cmake-cookbook/chapter-03/recipe-01/example/build