Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64541

Re: Using a static library in a C extension for Python

From Christian Gollwitzer <auriocus@gmx.de>
Newsgroups comp.lang.python
Subject Re: Using a static library in a C extension for Python
Date 2014-01-23 00:00 +0100
Organization A noiseless patient Spider
Message-ID <lbpij9$rsi$1@dont-email.me> (permalink)
References <52dfa51e$0$3621$426a74cc@news.free.fr>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Using a static library in a C extension for Python lgabiot <lgabiot@hotmail.com> - 2014-01-22 12:01 +0100
  Re: Using a static library in a C extension for Python 88888 Dihedral <dihedral88888@gmail.com> - 2014-01-22 09:31 -0800
    Re: Using a static library in a C extension for Python lgabiot <lgabiot@hotmail.com> - 2014-01-22 20:22 +0100
      Re: Using a static library in a C extension for Python Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-01-23 11:09 +1300
        Re: Using a static library in a C extension for Python lgabiot <lgabiot@hotmail.com> - 2014-01-23 03:12 +0100
      Re: Using a static library in a C extension for Python 88888 Dihedral <dihedral88888@gmail.com> - 2014-01-23 04:21 -0800
  Re: Using a static library in a C extension for Python Christian Gollwitzer <auriocus@gmx.de> - 2014-01-23 00:00 +0100
    Re: Using a static library in a C extension for Python lgabiot <lgabiot@hotmail.com> - 2014-01-23 03:17 +0100
    Re: Using a static library in a C extension for Python Chris Angelico <rosuav@gmail.com> - 2014-01-23 13:33 +1100
      Re: Using a static library in a C extension for Python lgabiot <lgabiot@hotmail.com> - 2014-01-23 08:41 +0100
      Re: Using a static library in a C extension for Python lgabiot <laurent.gabiot@gmail.com> - 2014-01-23 08:41 +0100

csiph-web