mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-22 13:15:12 +00:00
Remove obsolete example reference (#457)
* Remove obsolete example reference * Make example fully-working (except for #includes) Fixes #456.
This commit is contained in:
parent
18e9590ca9
commit
fb7c9fd326
39
docs/faq.rst
39
docs/faq.rst
@ -93,8 +93,10 @@ and the binding code
|
|||||||
How can I reduce the build time?
|
How can I reduce the build time?
|
||||||
================================
|
================================
|
||||||
|
|
||||||
It's good practice to split binding code over multiple files, as is done in
|
It's good practice to split binding code over multiple files, as in the
|
||||||
the included file :file:`example/example.cpp`.
|
following example:
|
||||||
|
|
||||||
|
:file:`example.cpp`:
|
||||||
|
|
||||||
.. code-block:: cpp
|
.. code-block:: cpp
|
||||||
|
|
||||||
@ -107,14 +109,41 @@ the included file :file:`example/example.cpp`.
|
|||||||
|
|
||||||
init_ex1(m);
|
init_ex1(m);
|
||||||
init_ex2(m);
|
init_ex2(m);
|
||||||
|
|
||||||
/* ... */
|
/* ... */
|
||||||
|
|
||||||
return m.ptr();
|
return m.ptr();
|
||||||
}
|
}
|
||||||
|
|
||||||
The various ``init_ex`` functions should be contained in separate files that
|
:file:`ex1.cpp`:
|
||||||
can be compiled independently from another. Following this approach will
|
|
||||||
|
.. code-block:: cpp
|
||||||
|
|
||||||
|
void init_ex1(py::module &m) {
|
||||||
|
m.def("add", [](int a, int b) { return a + b; });
|
||||||
|
}
|
||||||
|
|
||||||
|
:file:`ex2.cpp`:
|
||||||
|
|
||||||
|
.. code-block:: cpp
|
||||||
|
|
||||||
|
void init_ex1(py::module &m) {
|
||||||
|
m.def("sub", [](int a, int b) { return a - b; });
|
||||||
|
}
|
||||||
|
|
||||||
|
:command:`python`:
|
||||||
|
|
||||||
|
.. code-block:: pycon
|
||||||
|
|
||||||
|
>>> import example
|
||||||
|
>>> example.add(1, 2)
|
||||||
|
3
|
||||||
|
>>> example.sub(1, 1)
|
||||||
|
0
|
||||||
|
|
||||||
|
As shown above, the various ``init_ex`` functions should be contained in
|
||||||
|
separate files that can be compiled independently from one another, and then
|
||||||
|
linked together into the same final shared object. Following this approach
|
||||||
|
will:
|
||||||
|
|
||||||
1. reduce memory requirements per compilation unit.
|
1. reduce memory requirements per compilation unit.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user