[Pyrex] Dynamic wrapping of external C libraries?

Jim Kleckner jek-gmane at kleckner.net
Sat Oct 27 09:09:56 CEST 2007


Greg Ewing wrote:
> Jim Kleckner wrote:
>> You would like to link the library functions into the shared
>> object of the wrapper pyx and perform the dynamic linkages for
>> them.

> Note that you would only ever have to do this if the library
> in question were being statically linked. If it's dynamically
> linked, any other module that might want to use the API
> could just link directly with the library itself.

True.  I set up the example (attached) to demonstrate the
other way of getting the code into a shared object and
specifying it to the linker for the pyx-derived shared
objects.

This looks perfect on the face of it but in practice
I find that the executable path or load library path
needs to include every directory containing a shared
object for linkage to work.  Alternatively, you could
single out the shared objects and manually put them in
a directory that is on the path.  But it really breaks
the nice packaging of the packages/modules.

I would much rather statically link the wrapped code
into the extension because the dlopen methods of Python
will guarantee that the code is found (dlopen isn't used
if you link directly, path-based methods are).


For convenience, the key portion of the setup.py that
specifies the dynamic library is reproduced here:

ext_modules = [
     distutils.extension.Extension("libsquare",  ["libsquare.c", ], ),
     distutils.extension.Extension("testcimport.module", 
["testcimport.module.pyx", ],  library_dirs=['./',], 
libraries=['libsquare',], ),
     distutils.extension.Extension("testcimport.usetest", 
["testcimport.usetest.pyx", ], library_dirs=['./',], 
libraries=['libsquare',], ),
],

-------------- next part --------------
A non-text attachment was scrubbed...
Name: testcimport.tgz
Type: application/x-compressed
Size: 2139 bytes
Desc: not available
Url : http://lists.copyleft.no/pipermail/pyrex/attachments/20071027/21d803c7/attachment.bin 


More information about the Pyrex mailing list