Commit 5cbfda5b by Ivan Smirnov Committed by Dean Moldovan

Update build commands for Linux / OS X in the docs (#907)

parent 37de2da9
...@@ -73,6 +73,8 @@ For brevity, all code examples assume that the following two lines are present: ...@@ -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. Some features may require additional headers, but those will be specified as needed.
.. _simple_example:
Creating bindings for a simple function Creating bindings for a simple function
======================================= =======================================
...@@ -120,23 +122,31 @@ generates binding code that exposes the ``add()`` function to Python. ...@@ -120,23 +122,31 @@ generates binding code that exposes the ``add()`` function to Python.
approach and the used syntax are borrowed from Boost.Python, though the approach and the used syntax are borrowed from Boost.Python, though the
underlying implementation is very different. underlying implementation is very different.
pybind11 is a header-only-library, hence it is not necessary to link against 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 any special libraries and there are no intermediate (magic) translation steps.
build file discussed in section :ref:`cmake`. On Linux and Mac OS, the above On Linux, the above example can be compiled using the following command:
example can be compiled using the following command
.. code-block:: bash .. 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 .. _python_example: https://github.com/pybind/python_example
that can considerably reduce the size of the created binary. Refer to section .. _cmake_example: https://github.com/pybind/cmake_example
:ref:`cmake` for a detailed example of a suitable cross-platform CMake-based
build system.
Assuming that the created file :file:`example.so` (:file:`example.pyd` on Windows) Building the above C++ code will produce a binary module file that can be
is located in the current directory, the following interactive Python session imported to Python. Assuming that the compiled module is located in the
shows how to load and execute the example. current directory, the following interactive Python session shows how to
load and execute the example:
.. code-block:: pycon .. code-block:: pycon
......
.. _compiling:
Build systems Build systems
############# #############
...@@ -207,6 +209,59 @@ information about usage in C++, see :doc:`/advanced/embedding`. ...@@ -207,6 +209,59 @@ information about usage in C++, see :doc:`/advanced/embedding`.
add_executable(example main.cpp) add_executable(example main.cpp)
target_link_libraries(example PRIVATE pybind11::embed) target_link_libraries(example PRIVATE pybind11::embed)
.. _building_manually:
Building manually
=================
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, you can compile an example such as the one given in
:ref:`simple_example` using the following command:
.. code-block:: bash
$ c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix`
The flags given here assume that you're using Python 3. For Python 2, just
change the executable appropriately (to ``python`` or ``python2``).
The ``python3 -m pybind11 --includes`` command fetches the include paths for
both pybind11 and Python headers. This assumes that pybind11 has been installed
using ``pip`` or ``conda``. If it hasn't, you can also manually specify
``-I <path-to-pybind11>/include`` together with the Python includes path
``python3-config --includes``.
Note that Python 2.7 modules don't use a special suffix, so you should simply
use ``example.so`` instead of ``example`python3-config --extension-suffix```.
Besides, the ``--extension-suffix`` option may or may not be available, depending
on the distribution; in the latter case, the module extension can be manually
set to ``.so``.
On Mac OS: the build command is almost the same but it also requires passing
the ``-undefined dynamic_lookup`` flag so as to ignore missing symbols when
building the module:
.. code-block:: bash
$ c++ -O3 -Wall -shared -std=c++11 -undefined dynamic_lookup `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix`
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 that works on all platforms including Windows.
.. note::
On Linux and macOS, it's better to (intentionally) not link against
``libpython``. The symbols will be resolved when the extension library
is loaded into a Python binary. This is preferable because you might
have several different installations of a given Python version (e.g. the
system-provided Python, and one that ships with a piece of commercial
software). In this way, the plugin will work with both versions, instead
of possibly importing a second Python library into a process that already
contains one (which will lead to a segfault).
Generating binding code automatically Generating binding code automatically
===================================== =====================================
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment