2019-05-17 06:55:34 -03:00
|
|
|
.. highlight:: c
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. _building:
|
|
|
|
|
2015-07-03 06:49:15 -03:00
|
|
|
*****************************
|
|
|
|
Building C and C++ Extensions
|
|
|
|
*****************************
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2015-07-03 06:49:15 -03:00
|
|
|
A C extension for CPython is a shared library (e.g. a ``.so`` file on Linux,
|
|
|
|
``.pyd`` on Windows), which exports an *initialization function*.
|
|
|
|
|
|
|
|
To be importable, the shared library must be available on :envvar:`PYTHONPATH`,
|
|
|
|
and must be named after the module name, with an appropriate extension.
|
2022-07-25 10:50:46 -03:00
|
|
|
When using setuptools, the correct filename is generated automatically.
|
2015-07-03 06:49:15 -03:00
|
|
|
|
|
|
|
The initialization function has the signature:
|
|
|
|
|
|
|
|
.. c:function:: PyObject* PyInit_modulename(void)
|
|
|
|
|
2022-07-05 06:16:10 -03:00
|
|
|
It returns either a fully initialized module, or a :c:type:`PyModuleDef`
|
2015-07-03 06:49:15 -03:00
|
|
|
instance. See :ref:`initializing-modules` for details.
|
|
|
|
|
2019-05-17 06:55:34 -03:00
|
|
|
.. highlight:: python
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2015-07-03 06:49:15 -03:00
|
|
|
For modules with ASCII-only names, the function must be named
|
|
|
|
``PyInit_<modulename>``, with ``<modulename>`` replaced by the name of the
|
|
|
|
module. When using :ref:`multi-phase-initialization`, non-ASCII module names
|
|
|
|
are allowed. In this case, the initialization function name is
|
|
|
|
``PyInitU_<modulename>``, with ``<modulename>`` encoded using Python's
|
|
|
|
*punycode* encoding with hyphens replaced by underscores. In Python::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2015-07-03 06:49:15 -03:00
|
|
|
def initfunc_name(name):
|
|
|
|
try:
|
|
|
|
suffix = b'_' + name.encode('ascii')
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
suffix = b'U_' + name.encode('punycode').replace(b'-', b'_')
|
|
|
|
return b'PyInit' + suffix
|
|
|
|
|
|
|
|
It is possible to export multiple modules from a single shared library by
|
|
|
|
defining multiple initialization functions. However, importing them requires
|
|
|
|
using symbolic links or a custom importer, because by default only the
|
|
|
|
function corresponding to the filename is found.
|
2016-04-09 03:08:05 -03:00
|
|
|
See the *"Multiple modules in one library"* section in :pep:`489` for details.
|
2015-07-03 06:49:15 -03:00
|
|
|
|
|
|
|
|
2019-05-17 06:55:34 -03:00
|
|
|
.. highlight:: c
|
2015-07-03 06:49:15 -03:00
|
|
|
|
2023-08-16 18:06:56 -03:00
|
|
|
.. _install-index:
|
2022-07-25 10:50:46 -03:00
|
|
|
.. _setuptools-index:
|
2015-07-03 06:49:15 -03:00
|
|
|
|
2022-07-25 10:50:46 -03:00
|
|
|
Building C and C++ Extensions with setuptools
|
|
|
|
=============================================
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2022-07-25 10:50:46 -03:00
|
|
|
Python 3.12 and newer no longer come with distutils. Please refer to the
|
|
|
|
``setuptools`` documentation at
|
|
|
|
https://setuptools.readthedocs.io/en/latest/setuptools.html
|
|
|
|
to learn more about how build and distribute C/C++ extensions with setuptools.
|