Chapter 3 Detecting External Libraries and Programs
3.1 Detecting the Python interpreter
find_package
can be used to find a package located somewhere in your system. The CMake module which takes effect isFind<name>.cmake
whenfind_package(<name>)
is called.In addition to find the specified package,
find_package
will also set up some useful variables to indicate the result of finding:cmake1
2find_package(PythonInterp REQUIRED)
execute_process(COMMAND ${PYTHON_EXECUTABLE} "-c" "print('Hello, world!')")We can use
cmake_print_variables
fromCMakePrintHelpers
module for better print format.
3.2 Detecting the Python library
Use
find_package
to find both Python interpreter and library. To ensure their versions corresponds, we can specify the version infind_package
:cmake1
2
3
4
5
6
7
8
9
10find_package(PythonInterp REQUIRED)
find_package(PythonLibs ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR} EXACT REQUIRED)
target_include_directories(hello-embedded-python
PRIVATE
${PYTHON_INCLUDE_DIRS}
)
target_link_libraries(hello-embedded-python
PRIVATE
${PYTHON_LIBRARIES}
)
3.3 Detecting Python modules and packages
- We can use
execute_process
to execute commands in child processes, and get the return value, output and error info if failure.