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


Groups > comp.lang.python > #64492 > unrolled thread

Using a static library in a C extension for Python

Started bylgabiot <lgabiot@hotmail.com>
First post2014-01-22 12:01 +0100
Last post2014-01-23 08:41 +0100
Articles 11 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  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

#64492 — Using a static library in a C extension for Python

Fromlgabiot <lgabiot@hotmail.com>
Date2014-01-22 12:01 +0100
SubjectUsing a static library in a C extension for Python
Message-ID<52dfa51e$0$3621$426a74cc@news.free.fr>
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!

[toc] | [next] | [standalone]


#64517

From88888 Dihedral <dihedral88888@gmail.com>
Date2014-01-22 09:31 -0800
Message-ID<0510fa5b-41aa-4337-9e36-c851bc83426a@googlegroups.com>
In reply to#64492
On Wednesday, January 22, 2014 7:01:50 PM UTC+8, lgabiot wrote:
> 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.

[toc] | [prev] | [next] | [standalone]


#64529

Fromlgabiot <lgabiot@hotmail.com>
Date2014-01-22 20:22 +0100
Message-ID<52e01a8c$0$2929$426a34cc@news.free.fr>
In reply to#64517
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?

[toc] | [prev] | [next] | [standalone]


#64538

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2014-01-23 11:09 +1300
Message-ID<bkatsbFb3mlU1@mid.individual.net>
In reply to#64529
lgabiot wrote:
> Le 22/01/14 18:31, 88888 Dihedral a écrit :
>
>> Check the  C source code generated
>> by Pyrex ...
>>
> 
> 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.

-- 
Greg

[toc] | [prev] | [next] | [standalone]


#64551

Fromlgabiot <lgabiot@hotmail.com>
Date2014-01-23 03:12 +0100
Message-ID<52e07a8b$0$2041$426a34cc@news.free.fr>
In reply to#64538
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!!!


[toc] | [prev] | [next] | [standalone]


#64599

From88888 Dihedral <dihedral88888@gmail.com>
Date2014-01-23 04:21 -0800
Message-ID<d956e368-9066-44b2-afe3-38479745a5ec@googlegroups.com>
In reply to#64529
On Thursday, January 23, 2014 3:22:52 AM UTC+8, lgabiot wrote:
> 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?

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.

 

[toc] | [prev] | [next] | [standalone]


#64541

FromChristian Gollwitzer <auriocus@gmx.de>
Date2014-01-23 00:00 +0100
Message-ID<lbpij9$rsi$1@dont-email.me>
In reply to#64492
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

[toc] | [prev] | [next] | [standalone]


#64552

Fromlgabiot <lgabiot@hotmail.com>
Date2014-01-23 03:17 +0100
Message-ID<52e07bb2$0$2119$426a34cc@news.free.fr>
In reply to#64541
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).

[toc] | [prev] | [next] | [standalone]


#64556

FromChris Angelico <rosuav@gmail.com>
Date2014-01-23 13:33 +1100
Message-ID<mailman.5860.1390444399.18130.python-list@python.org>
In reply to#64541
On Thu, Jan 23, 2014 at 10:00 AM, Christian Gollwitzer <auriocus@gmx.de> wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#64578

Fromlgabiot <lgabiot@hotmail.com>
Date2014-01-23 08:41 +0100
Message-ID<52E0C78F.1030504@hotmail.com>
In reply to#64556
Le 23/01/14 03:33, Chris Angelico a écrit :
> On Thu, Jan 23, 2014 at 10:00 AM, Christian Gollwitzer <auriocus@gmx.de> wrote:
>> 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
>

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

[toc] | [prev] | [next] | [standalone]


#64579

Fromlgabiot <laurent.gabiot@gmail.com>
Date2014-01-23 08:41 +0100
Message-ID<mailman.5874.1390462872.18130.python-list@python.org>
In reply to#64556
Le 23/01/14 03:33, Chris Angelico a écrit :
> On Thu, Jan 23, 2014 at 10:00 AM, Christian Gollwitzer <auriocus@gmx.de> wrote:
>> 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
>

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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web