Using a static library in a C extension for Python
Christian Gollwitzer
auriocus at gmx.de
Wed Jan 22 18:00:56 EST 2014
Hi,
Am 22.01.14 12:01, schrieb lgabiot:
> Is it possible to link statically cairo to my extension, so that even if
> cairo is not installed on a computer, the code will run?
>
> I guess I would need to modify the setup.py file using distutils to
> compile cairo statically into my .so file?
I've done it for a similar problem (linking HDF statically including its
dependencies zlib and libjpeg), but I did write the Makefile by hand
instead of using distutils.
First, you need to compile a static version of cairo. I.e., you probably
have got some libcairo.2.dylib or similar. This can't be used, you must
end up with a libcairo.a archive, by passing "--disable-shared
--enable-static" to the configure script. For this to work, the code
must be position-independent ("--with-pic" in autoconf). This is a no-op
on OSX, where all code is position-independent, but is important if you
plan to port it to Linux. Then, during the linking step of your
extension, you need to link against that statically compiled version of
libcairo. In a Makefile, you would pass "-L/path/to/thelibrary -lcairo"
to the compiler invocation for the linking step. I don't know distutils
enough, but maybe you can either pass "LDFLAGS" to it to do this or
directly point to that version of cairo.
> Or am I completely wrong?
I'm a big fan of static linkage in that case, too.
There might be another issue with the license of the library. Cairo is
both LGPL and MPL. For LGPL, only dynamic linking is without doubt, for
MPL it seems to be accepted to link statically. It all depends on
whether you plan to pass on the binary to another person.
To circumvent this problem, it might be feasable to just install
libcairo along with you extension.
Then, the exact same version of python must be used on both computers.
Since the extension loading mechanism completely relies on the OS
dynamic linker, it is not possible to load an extension into a different
version of python than it was built for. Sadly, nobody implemented a
system like Tcl's stubs, where an array of function pointers gets passed
to the extension. This system allows to load an extension into later
versions of the host program.
Christian
More information about the Python-list
mailing list