Skip to content

Commit a2d79ee

Browse files
committed
multiple files extension documentation improved
1 parent 6b773b7 commit a2d79ee

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

docs/source/numpy_capi.rst

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ Thus the basic skeleton of the module looks like:
2323

2424
.. code::
2525
26-
#include "pybind11/pybind11.h"
2726
#define FORCE_IMPORT_ARRAY
2827
#include "xtensor-python/pyarray.hpp"
2928
@@ -37,9 +36,45 @@ Thus the basic skeleton of the module looks like:
3736
Extension module with multiple files
3837
------------------------------------
3938

40-
If the extension module contain many source files that include ``xtensor-python`` header files, the previous points are still
41-
required. However, the source files that don't contain the initializing code of the module can directly include ``xtensor-python``
42-
header files.
39+
If the extension module contains many source files that include ``xtensor-python`` header files, the previous points are still
40+
required. However, the symbol ``FORCE_IMPORT_ARRAY`` must be defined only once. The simplest is to define it int the file that
41+
contains the initializing code of the module, you can then directly include ``xtensor-python`` headers in other files. Let's
42+
illustrate this with an extension modules containing the following files:
43+
44+
- ``main.cpp``: initializing code of the module
45+
- ``image.hpp``: declaration of the ``image`` class embedding an ``xt::pyarray`` object
46+
- ``image.cpp``: implementation of the ``image`` class
47+
48+
The basic skeleton of the module looks like:
49+
50+
.. code::
51+
52+
// image.hpp
53+
// Do NOT define FORCE_IMPORT_ARRAY here
54+
#include "xtensor-python/pyarray.hpp"
55+
56+
class image
57+
{
58+
// ....
59+
private:
60+
xt::pyarray<double> m_data;
61+
};
62+
63+
// image.cpp
64+
// Do NOT define FORCE_IMPORT_ARRAY here
65+
#include "image.hpp"
66+
// definition of the image class
67+
68+
// main.cpp
69+
// FORCE_IMPORT_ARRAY must be define ONCE, BEFORE including
70+
// any header from xtensor-python (even indirectly)
71+
#define FORCE_IMPORT_ARRAY
72+
#include "image.hpp"
73+
PYBIND11_MODULE(plugin_name, m)
74+
{
75+
xt::import_numpy();
76+
//...
77+
}
4378
4479
4580
Using other extension modules

0 commit comments

Comments
 (0)