Last Updated: February 25, 2016
·
1.961K
· dtcaciuc

Easy static/shared libraries with distutils

Even though, in retrospect, the API docs were always there, somehow looking at distutils innards led me in an awkward direction for the longest time. Given some source files, the actual basic use case is very simple:

from distutils.ccompiler import new_compiler

# Create compiler with default options
c = new_compiler()
workdir = "."

# Optionally add include directories etc.
# c.add_include_dir("some/include")

# Compile into .o files
objects = c.compile(["a.c", "b.c"])

# Create static or shared library
c.create_static_lib(objects, "foo", output_dir=workdir)
c.link_shared_lib(objects, "foo", output_dir=workdir)