X Tutup
Skip to content

Commit 71bca34

Browse files
committed
Issue #1040439: better document how to compile and link an embedded Python interpreter.
Still lacks docs for Windows (anyone?).
1 parent a74f8ef commit 71bca34

File tree

1 file changed

+47
-31
lines changed

1 file changed

+47
-31
lines changed

Doc/extending/embedding.rst

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ The code to run a function defined in a Python script is:
133133

134134
This code loads a Python script using ``argv[1]``, and calls the function named
135135
in ``argv[2]``. Its integer arguments are the other values of the ``argv``
136-
array. If you compile and link this program (let's call the finished executable
137-
:program:`call`), and use it to execute a Python script, such as::
136+
array. If you :ref:`compile and link <compiling>` this program (let's call
137+
the finished executable :program:`call`), and use it to execute a Python
138+
script, such as::
138139

139140
def multiply(a,b):
140141
print("Will compute", a, "times", b)
@@ -257,37 +258,52 @@ write the main program in C++, and use the C++ compiler to compile and link your
257258
program. There is no need to recompile Python itself using C++.
258259

259260

260-
.. _link-reqs:
261+
.. _compiling:
261262

262-
Linking Requirements
263-
====================
264-
265-
While the :program:`configure` script shipped with the Python sources will
266-
correctly build Python to export the symbols needed by dynamically linked
267-
extensions, this is not automatically inherited by applications which embed the
268-
Python library statically, at least on Unix. This is an issue when the
269-
application is linked to the static runtime library (:file:`libpython.a`) and
270-
needs to load dynamic extensions (implemented as :file:`.so` files).
271-
272-
The problem is that some entry points are defined by the Python runtime solely
273-
for extension modules to use. If the embedding application does not use any of
274-
these entry points, some linkers will not include those entries in the symbol
275-
table of the finished executable. Some additional options are needed to inform
276-
the linker not to remove these symbols.
277-
278-
Determining the right options to use for any given platform can be quite
279-
difficult, but fortunately the Python configuration already has those values.
280-
To retrieve them from an installed Python interpreter, start an interactive
281-
interpreter and have a short session like this::
263+
Compiling and Linking under Unix-like systems
264+
=============================================
282265

283-
>>> import distutils.sysconfig
284-
>>> distutils.sysconfig.get_config_var('LINKFORSHARED')
266+
It is not necessarily trivial to find the right flags to pass to your
267+
compiler (and linker) in order to embed the Python interpreter into your
268+
application, particularly because Python needs to load library modules
269+
implemented as C dynamic extensions (:file:`.so` files) linked against
270+
it.
271+
272+
To find out the required compiler and linker flags, you can execute the
273+
:file:`python{X.Y}-config` script which is generated as part of the
274+
installation process (a generic :file:`python3-config` script is also
275+
available). This script has several options, of which the following will
276+
be directly useful to you:
277+
278+
* ``pythonX.Y-config --cflags`` will give you the recommended flags when
279+
compiling::
280+
281+
$ /opt/bin/python3.2-config --cflags
282+
-I/opt/include/python3.2m -I/opt/include/python3.2m -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes
283+
284+
* ``pythonX.Y-config --ldflags`` will give you the recommended flags when
285+
linking::
286+
287+
$ /opt/bin/python3.2-config --ldflags
288+
-I/opt/lib/python3.2/config-3.2m -lpthread -ldl -lutil -lm -lpython3.2m -Xlinker -export-dynamic
289+
290+
.. note::
291+
To avoid confusion between several Python installations (and especially
292+
between the system Python and your own compiled Python), it is recommended
293+
that you use the absolute path to :file:`python{X.Y}-config`, as in the above
294+
example.
295+
296+
If this procedure doesn't work for you (it is not guaranteed to work for
297+
all Unix-like platforms; however, we welcome bug reports at
298+
http://bugs.python.org), you will have to read your system's documentation
299+
about dynamic linking and/or examine Python's Makefile and compilation
300+
options. In this case, the :mod:`sysconfig` module is a useful tool to
301+
programmatically extract the configuration values that you will want to
302+
combine together::
303+
304+
>>> import sysconfig
305+
>>> sysconfig.get_config_var('LINKFORSHARED')
285306
'-Xlinker -export-dynamic'
286307

287-
.. index:: module: distutils.sysconfig
288-
289-
The contents of the string presented will be the options that should be used.
290-
If the string is empty, there's no need to add any additional options. The
291-
:const:`LINKFORSHARED` definition corresponds to the variable of the same name
292-
in Python's top-level :file:`Makefile`.
293308

309+
.. XXX similar documentation for Windows missing

0 commit comments

Comments
 (0)
X Tutup