C/C++ extensions
The previous chapter and earlier sections in this chapter have already covered the compilation of C/C++ components lightly, but this topic is complicated enough to warrant its own section with more in-depth explanations.
For convenience, we will start with a basic setup.py
file that compiles a C extension:
import setuptools
sum_of_squares = setuptools.Extension('sum_of_squares', sources=[
# Get the relative path to sum_of_squares.c
str(PROJECT_PATH / 'sum_of_squares.c'),
])
setuptools.setup(
name='T_04_C_extensions',
version='0.1.0',
ext_modules=[sum_of_squares],
)
Before you start with these extensions, you should learn the following setup.py
commands:
build_ext
: This command builds the C/C++ extension so it can be used when the package is installed in development/editable mode.clean
: This cleans the results from thebuild
command. This is generally not...