@@ -14,13 +14,57 @@ Consider the following C++ code:
14
14
:language: cpp
15
15
16
16
There are several options to build the module,
17
- whereby we will CMake here with the following ``CMakeLists.txt ``:
17
+ whereby we will use * CMake * here with the following ``CMakeLists.txt ``:
18
18
19
19
:download: `CMakeLists.txt <examples/readme_example_1/CMakeLists.txt >`
20
20
21
21
.. literalinclude :: examples/readme_example_1/CMakeLists.txt
22
22
:language: cmake
23
23
24
+ .. tip ::
25
+
26
+ There is a potential pitfall here, centered around the fact that *CMake *
27
+ has a 'new' *FindPython * and a 'classic' *FindPythonLibs *.
28
+ We here use *FindPython * because of its ability to find the NumPy headers,
29
+ that we need for *xtensor-python *.
30
+
31
+ This has the consequence that when we want to force *CMake *
32
+ to use a specific *Python * executable, we have to use something like
33
+
34
+ .. code-block :: none
35
+
36
+ cmake -Bbuild -DPython_EXECUTABLE=`which python`
37
+
38
+ whereby it is crucial that one uses the correct case ``Python_EXECUTABLE ``, as:
39
+
40
+ .. code-block :: none
41
+
42
+ Python_EXECUTABLE <-> FindPython
43
+ PYTHON_EXECUTABLE <-> FindPythonLibs
44
+
45
+ (remember that *CMake * is **case-sensitive **!).
46
+
47
+ Now, since we use *FindPython * because of *xtensor-python * we also want *pybind11 *
48
+ to use *FindPython *
49
+ (and not the classic *FindPythonLibs *,
50
+ since we want to specify the *Python * executable only once).
51
+ To this end we have to make sure to do things in the correct order, which is
52
+
53
+ .. code-block :: cmake
54
+
55
+ find_package(Python REQUIRED COMPONENTS Interpreter Development NumPy)
56
+ find_package(pybind11 REQUIRED CONFIG)
57
+
58
+ (i.e. one finds *Python * **before ** *pybind11 *).
59
+ See the `pybind11 documentation <https://pybind11.readthedocs.io/en/latest/cmake/index.html#new-findpython-mode >`_.
60
+
61
+ In addition, be sure to use a quite recent *CMake * version,
62
+ by starting your ``CMakeLists.txt `` for example with
63
+
64
+ .. code-block :: cmake
65
+
66
+ cmake_minimum_required(VERSION 3.18..3.20)
67
+
24
68
Then we can test the module:
25
69
26
70
:download: `example.py <examples/readme_example_1/example.py >`
@@ -33,7 +77,7 @@ Then we can test the module:
33
77
Since we did not install the module,
34
78
we should compile and run the example from the same folder.
35
79
To install, please consult
36
- `this pybind11 / CMake example <https://github.com/pybind/cmake_example >`_.
80
+ `this * pybind11* / * CMake* example <https://github.com/pybind/cmake_example >`_.
37
81
38
82
39
83
Type restriction with SFINAE
@@ -78,13 +122,15 @@ For the Python module we just have to specify the template to be
78
122
.. literalinclude :: examples/sfinae/python.cpp
79
123
:language: cpp
80
124
81
- We will again use CMake to compile, with the following ``CMakeLists.txt ``:
125
+ We will again use * CMake * to compile, with the following ``CMakeLists.txt ``:
82
126
83
127
:download: `CMakeLists.txt <examples/sfinae/CMakeLists.txt >`
84
128
85
129
.. literalinclude :: examples/sfinae/CMakeLists.txt
86
130
:language: cmake
87
131
132
+ (see *CMake * tip above).
133
+
88
134
Then we can test the module:
89
135
90
136
:download: `example.py <examples/readme_example_1/example.py >`
@@ -97,4 +143,5 @@ Then we can test the module:
97
143
Since we did not install the module,
98
144
we should compile and run the example from the same folder.
99
145
To install, please consult
100
- `this pybind11 / CMake example <https://github.com/pybind/cmake_example >`_.
146
+ `this *pybind11* / *CMake* example <https://github.com/pybind/cmake_example >`_.
147
+ **Tip **: take care to modify that example with the correct *CMake * case ``Python_EXECUTABLE ``.
0 commit comments