From fb7c9fd3260f306b267acbb3450512cb7c9a4ace Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Sat, 22 Oct 2016 12:54:33 -0400 Subject: [PATCH] Remove obsolete example reference (#457) * Remove obsolete example reference * Make example fully-working (except for #includes) Fixes #456. --- docs/faq.rst | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/docs/faq.rst b/docs/faq.rst index 538f8dcc2..cf7a650b7 100644 --- a/docs/faq.rst +++ b/docs/faq.rst @@ -93,8 +93,10 @@ and the binding code How can I reduce the build time? ================================ -It's good practice to split binding code over multiple files, as is done in -the included file :file:`example/example.cpp`. +It's good practice to split binding code over multiple files, as in the +following example: + +:file:`example.cpp`: .. code-block:: cpp @@ -107,14 +109,41 @@ the included file :file:`example/example.cpp`. init_ex1(m); init_ex2(m); - /* ... */ return m.ptr(); } -The various ``init_ex`` functions should be contained in separate files that -can be compiled independently from another. Following this approach will +:file:`ex1.cpp`: + +.. 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.