Skip to content

Commit 5cbfda5

Browse files
aldanordean0x7d
authored andcommitted
Update build commands for Linux / OS X in the docs (#907)
1 parent 37de2da commit 5cbfda5

File tree

2 files changed

+77
-12
lines changed

2 files changed

+77
-12
lines changed

docs/basics.rst

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ For brevity, all code examples assume that the following two lines are present:
7373
7474
Some features may require additional headers, but those will be specified as needed.
7575

76+
.. _simple_example:
77+
7678
Creating bindings for a simple function
7779
=======================================
7880

@@ -120,23 +122,31 @@ generates binding code that exposes the ``add()`` function to Python.
120122
approach and the used syntax are borrowed from Boost.Python, though the
121123
underlying implementation is very different.
122124

123-
pybind11 is a header-only-library, hence it is not necessary to link against
124-
any special libraries (other than Python itself). On Windows, use the CMake
125-
build file discussed in section :ref:`cmake`. On Linux and Mac OS, the above
126-
example can be compiled using the following command
125+
pybind11 is a header-only library, hence it is not necessary to link against
126+
any special libraries and there are no intermediate (magic) translation steps.
127+
On Linux, the above example can be compiled using the following command:
127128

128129
.. code-block:: bash
129130
130-
$ c++ -O3 -shared -std=c++11 -I <path-to-pybind11>/include `python-config --cflags --ldflags` example.cpp -o example.so
131+
$ c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix`
132+
133+
For more details on the required compiler flags on Linux and MacOS, see
134+
:ref:`building_manually`. For complete cross-platform compilation instructions,
135+
refer to the :ref:`compiling` page.
136+
137+
The `python_example`_ and `cmake_example`_ repositories are also a good place
138+
to start. They are both complete project examples with cross-platform build
139+
systems. The only difference between the two is that `python_example`_ uses
140+
Python's ``setuptools`` to build the module, while `cmake_example`_ uses CMake
141+
(which may be preferable for existing C++ projects).
131142

132-
In general, it is advisable to include several additional build parameters
133-
that can considerably reduce the size of the created binary. Refer to section
134-
:ref:`cmake` for a detailed example of a suitable cross-platform CMake-based
135-
build system.
143+
.. _python_example: https://github.com/pybind/python_example
144+
.. _cmake_example: https://github.com/pybind/cmake_example
136145

137-
Assuming that the created file :file:`example.so` (:file:`example.pyd` on Windows)
138-
is located in the current directory, the following interactive Python session
139-
shows how to load and execute the example.
146+
Building the above C++ code will produce a binary module file that can be
147+
imported to Python. Assuming that the compiled module is located in the
148+
current directory, the following interactive Python session shows how to
149+
load and execute the example:
140150

141151
.. code-block:: pycon
142152

docs/compiling.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _compiling:
2+
13
Build systems
24
#############
35

@@ -207,6 +209,59 @@ information about usage in C++, see :doc:`/advanced/embedding`.
207209
add_executable(example main.cpp)
208210
target_link_libraries(example PRIVATE pybind11::embed)
209211
212+
.. _building_manually:
213+
214+
Building manually
215+
=================
216+
217+
pybind11 is a header-only library, hence it is not necessary to link against
218+
any special libraries and there are no intermediate (magic) translation steps.
219+
220+
On Linux, you can compile an example such as the one given in
221+
:ref:`simple_example` using the following command:
222+
223+
.. code-block:: bash
224+
225+
$ c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix`
226+
227+
The flags given here assume that you're using Python 3. For Python 2, just
228+
change the executable appropriately (to ``python`` or ``python2``).
229+
230+
The ``python3 -m pybind11 --includes`` command fetches the include paths for
231+
both pybind11 and Python headers. This assumes that pybind11 has been installed
232+
using ``pip`` or ``conda``. If it hasn't, you can also manually specify
233+
``-I <path-to-pybind11>/include`` together with the Python includes path
234+
``python3-config --includes``.
235+
236+
Note that Python 2.7 modules don't use a special suffix, so you should simply
237+
use ``example.so`` instead of ``example`python3-config --extension-suffix```.
238+
Besides, the ``--extension-suffix`` option may or may not be available, depending
239+
on the distribution; in the latter case, the module extension can be manually
240+
set to ``.so``.
241+
242+
On Mac OS: the build command is almost the same but it also requires passing
243+
the ``-undefined dynamic_lookup`` flag so as to ignore missing symbols when
244+
building the module:
245+
246+
.. code-block:: bash
247+
248+
$ c++ -O3 -Wall -shared -std=c++11 -undefined dynamic_lookup `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix`
249+
250+
In general, it is advisable to include several additional build parameters
251+
that can considerably reduce the size of the created binary. Refer to section
252+
:ref:`cmake` for a detailed example of a suitable cross-platform CMake-based
253+
build system that works on all platforms including Windows.
254+
255+
.. note::
256+
257+
On Linux and macOS, it's better to (intentionally) not link against
258+
``libpython``. The symbols will be resolved when the extension library
259+
is loaded into a Python binary. This is preferable because you might
260+
have several different installations of a given Python version (e.g. the
261+
system-provided Python, and one that ships with a piece of commercial
262+
software). In this way, the plugin will work with both versions, instead
263+
of possibly importing a second Python library into a process that already
264+
contains one (which will lead to a segfault).
210265

211266
Generating binding code automatically
212267
=====================================

0 commit comments

Comments
 (0)