avatar

Catalog
CMake Cookbook Notes 3: Detecting External Libraries and Programs

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 is Find<name>.cmake when find_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:

    cmake
    1
    2
    find_package(PythonInterp REQUIRED)
    execute_process(COMMAND ${PYTHON_EXECUTABLE} "-c" "print('Hello, world!')")
  • We can use cmake_print_variables from CMakePrintHelpers 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 in find_package:

    cmake
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    find_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.
Author: Gusabary
Link: http://gusabary.cn/2020/10/17/CMake-CookBook-Notes/CMake-Cookbook-Notes-3/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.

Comment