The code for this recipe is available at https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-09/recipe-05 and has a C++ example. The recipe is valid with CMake version 3.11 (and higher) and has been tested on GNU/Linux, macOS, and Windows.
In the previous recipe, we have used Boost.Python to interface Python with C(++). In this recipe, we will try to interface Python with C++ using pybind11 as a lightweight alternative that makes use of C++11 features and therefore requires a compiler with C++11 support. As an additional variation to the previous recipe we will demonstrate how to fetch the pybind11 dependency at configure time and build our project including a Python interface using the FetchContent approach, which we met in Chapter 4, Creating and Running Tests, Recipe 3, Define a unit test and linking against Google Test...