diff options
Diffstat (limited to 'ext/pybind11/docs/basics.rst')
-rw-r--r-- | ext/pybind11/docs/basics.rst | 58 |
1 files changed, 31 insertions, 27 deletions
diff --git a/ext/pybind11/docs/basics.rst b/ext/pybind11/docs/basics.rst index 33c60049d..447250ed9 100644 --- a/ext/pybind11/docs/basics.rst +++ b/ext/pybind11/docs/basics.rst @@ -73,6 +73,8 @@ For brevity, all code examples assume that the following two lines are present: Some features may require additional headers, but those will be specified as needed. +.. _simple_example: + Creating bindings for a simple function ======================================= @@ -96,25 +98,21 @@ a file named :file:`example.cpp` with the following contents: return i + j; } - namespace py = pybind11; - - PYBIND11_PLUGIN(example) { - py::module m("example", "pybind11 example plugin"); + PYBIND11_MODULE(example, m) { + m.doc() = "pybind11 example plugin"; // optional module docstring m.def("add", &add, "A function which adds two numbers"); - - return m.ptr(); } .. [#f1] In practice, implementation and binding code will generally be located in separate files. -The :func:`PYBIND11_PLUGIN` macro creates a function that will be called when an -``import`` statement is issued from within Python. The next line creates a -module named ``example`` (with the supplied docstring). The method -:func:`module::def` generates binding code that exposes the -``add()`` function to Python. The last line returns the internal Python object -associated with ``m`` to the Python interpreter. +The :func:`PYBIND11_MODULE` macro creates a function that will be called when an +``import`` statement is issued from within Python. The module name (``example``) +is given as the first macro argument (it should not be in quotes). The second +argument (``m``) defines a variable of type :class:`py::module <module>` which +is the main interface for creating bindings. The method :func:`module::def` +generates binding code that exposes the ``add()`` function to Python. .. note:: @@ -124,23 +122,31 @@ associated with ``m`` to the Python interpreter. approach and the used syntax are borrowed from Boost.Python, though the underlying implementation is very different. -pybind11 is a header-only-library, hence it is not necessary to link against -any special libraries (other than Python itself). On Windows, use the CMake -build file discussed in section :ref:`cmake`. On Linux and Mac OS, the above -example can be compiled using the following command +pybind11 is a header-only library, hence it is not necessary to link against +any special libraries and there are no intermediate (magic) translation steps. +On Linux, the above example can be compiled using the following command: .. code-block:: bash - $ c++ -O3 -shared -std=c++11 -I <path-to-pybind11>/include `python-config --cflags --ldflags` example.cpp -o example.so + $ c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix` + +For more details on the required compiler flags on Linux and MacOS, see +:ref:`building_manually`. For complete cross-platform compilation instructions, +refer to the :ref:`compiling` page. + +The `python_example`_ and `cmake_example`_ repositories are also a good place +to start. They are both complete project examples with cross-platform build +systems. The only difference between the two is that `python_example`_ uses +Python's ``setuptools`` to build the module, while `cmake_example`_ uses CMake +(which may be preferable for existing C++ projects). -In general, it is advisable to include several additional build parameters -that can considerably reduce the size of the created binary. Refer to section -:ref:`cmake` for a detailed example of a suitable cross-platform CMake-based -build system. +.. _python_example: https://github.com/pybind/python_example +.. _cmake_example: https://github.com/pybind/cmake_example -Assuming that the created file :file:`example.so` (:file:`example.pyd` on Windows) -is located in the current directory, the following interactive Python session -shows how to load and execute the example. +Building the above C++ code will produce a binary module file that can be +imported to Python. Assuming that the compiled module is located in the +current directory, the following interactive Python session shows how to +load and execute the example: .. code-block:: pycon @@ -261,12 +267,10 @@ converted using the function ``py::cast``. .. code-block:: cpp - PYBIND11_PLUGIN(example) { - py::module m("example", "pybind11 example plugin"); + PYBIND11_MODULE(example, m) { m.attr("the_answer") = 42; py::object world = py::cast("World"); m.attr("what") = world; - return m.ptr(); } These are then accessible from Python: |