What is the most conventional way to integrate C code into a Python library using distutils?

Many well-known python libraries are basically written in C (like tensorflow or numpy) because this apparently speeds things up a lot. I was able to very easily integrate a C function into python by reading this. Doing so I can finally use distutils to access the functions of the source.c file:

# setup.py

from distutils.core import setup, Extension

def main():
    setup(
        # All the other parameters...
        ext_modules=[ Extension("source", ["source.c"]) ]
        )

if __name__ == "__main__":
    main()

so that when i run python setup.py install i can install my library. However, what if i want to create a python-coded wrapper object for the functions inside source.c? Is there a way to do this without polluting the installed modules?
Wandering around the internet I have seen some seemingly simple solutions using shared libraries (.so). However I would need a solution that does not involve attaching the compiled C code, but one that compiles it the first time the program is run.

Answer

The shared libraries are the right way to go in this case. The distutils have ability to build the static libraries as follows:

from distutils.ccompiler import new_compiler

c = new_compiler()
workdir = "."
c.add_include_dir("./include")
objects = c.compile(["file1.c", "file2.c"])
c.link_shared_lib(objects, "mylibrary", output_dir=workdir)

This will generate the .so library in the working directory.

For example how it’s used in real setup see the following example

Leave a Reply

Your email address will not be published. Required fields are marked *