Using a static library in a C extension for Python


L

lgabiot

Hello,

working on OS X 10.8.5
Python 2.7

I've written a simple C extension for Python that uses the cairo graphic
library.
It works well, and I can call it from Python with no problem.
The only drawback is that I need to have the cairo library installed on
my system (so it seems my extension calls dynamically the cairo library).
I believe this because it doesn't work on a system where cairo is not
installed.

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?

Or am I completely wrong?

If someone has a hint...

thanks!
 
Ad

Advertisements

8

88888 Dihedral

Hello,



working on OS X 10.8.5

Python 2.7



I've written a simple C extension for Python that uses the cairo graphic

library.

It works well, and I can call it from Python with no problem.

The only drawback is that I need to have the cairo library installed on

my system (so it seems my extension calls dynamically the cairo library).

I believe this because it doesn't work on a system where cairo is not

installed.



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?



Or am I completely wrong?



If someone has a hint...



thanks!

Check the C source code generated
by Pyrex and check cython for what u
want, but I did try that out in any
mobile phone or flat panel
programming.
 
L

lgabiot

Le 22/01/14 18:31, 88888 Dihedral a écrit :
Check the C source code generated
by Pyrex and check cython for what u
want, but I did try that out in any
mobile phone or flat panel
programming.

Thanks a lot for your answer.

I didn't use Pyrex or other tool, but wrote myself the C python
wrapping, using the Python C/API documentation
(http://docs.python.org/2/c-api/). I then used the distutils tool (via a
setup.py file) to build my extension.
While there is several function in my C code, only one needed to be
accessed by python.

I'll check Cython then, but is there any tweaking of the setup.py file
using distutils that will help me compile the extension with the cairo
lib embedded into it?
 
G

Gregory Ewing

lgabiot said:
Le 22/01/14 18:31, 88888 Dihedral a écrit :


Thanks a lot for your answer.

We suspect that 88888 Dihedral is actually a bot,
so you're *probably* wasting your time attempting
to engage it in conversation.
 
L

lgabiot

Le 22/01/14 23:09, Gregory Ewing a écrit :
We suspect that 88888 Dihedral is actually a bot,
so you're *probably* wasting your time attempting
to engage it in conversation.

Thanks,
so that will be my first real experience of the Turing test!!!
 
L

lgabiot

thanks a lot for your very precise answer!

shortly, as I'm running out of time right now:
I've got here a lot of informations, so I'll dig in the directions you
gave me. It will be a good compiling exercise... (I'm really new at all
this).
 
Ad

Advertisements

C

Chris Angelico

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.

If you can tie in with your OS's package manager, that would solve all
of these problems. You get the OS-supplied Pythom and the OS-supplied
libcairo; grabbing libcairo-dev (or, on my Debian system,
libcairo2-dev to go with libcairo2) gets you what you need to build
your extension; you then might even package your extension the same
way, and then simply declare dependencies. Can save a HUGE amount of
trouble.

ChrisA
 
L

lgabiot

Le 23/01/14 03:33, Chris Angelico a écrit :
If you can tie in with your OS's package manager, that would solve all
of these problems. You get the OS-supplied Pythom and the OS-supplied
libcairo; grabbing libcairo-dev (or, on my Debian system,
libcairo2-dev to go with libcairo2) gets you what you need to build
your extension; you then might even package your extension the same
way, and then simply declare dependencies. Can save a HUGE amount of
trouble.

ChrisA

thank you very much for your answer, I'll work on your informations.
 
Ad

Advertisements

8

88888 Dihedral

Le 22/01/14 18:31, 88888 Dihedral a écrit :










Thanks a lot for your answer.



I didn't use Pyrex or other tool, but wrote myself the C python

wrapping, using the Python C/API documentation

(http://docs.python.org/2/c-api/). I then used the distutils tool (via a

setup.py file) to build my extension.

While there is several function in my C code, only one needed to be

accessed by python.



I'll check Cython then, but is there any tweaking of the setup.py file

using distutils that will help me compile the extension with the cairo

lib embedded into it?

There are some design concerns in
writing python scripts that might
be modified for fast execution speeds in some platforms.

Well, reducing basic operation
overheads in well factored critical
parts does help in getting the
desired results faster.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top