[Pyrex] sharing funcitons with distutils for .pyx

Stefan Behnel stefan_ml at behnel.de
Wed Sep 19 09:38:32 CEST 2007


Jim Kleckner wrote:
> The documentation says to add libraries in distutils
> for extensions with a "libraries" clause as in:
>      distutils.extension.Extension("pack1.mod2", ["mod2.pyx", ], 
> libraries=['mod1',], ),
> 
> I have a .pyx file that depends on functions defined in a .c
> file that is included with another .pyx, say it is "cfunc1" defined in 
> cfuncs.c
> 
> =======
>      ext_modules = [
>          distutils.extension.Extension("pack1.mod1", ["mod1.pyx", 
> "cfuncs.c", ]),
>          distutils.extension.Extension("pack1.mod2", ["mod2.pyx", ], 
> libraries=['mod1',], ),
>      ],
> =======
> 
> If I don't include "libraries=['mod1',]" then I get an undefined 
> reference to cfunc1 when linking mod2.dll.

You could use "cfuncs.c" as source file for both modules.


> If I include the 'mod1' library, then the linker gets -lmod1
> which looks for libmod1.dll which isn't there (it is called mod1.dll).
> 
> Is there some little trick to sharing functions between pyx extensions?
> I'm sure I'm missing something simple.

Cython has support for a public C-API that allows you to call functions
defined public in one module from a second module directly at the C level (and
without any hassle of linking and whatever).

http://www.cython.org/

Here is an example of an implementation of such functions (basically wrappers
in this case):

http://codespeak.net/svn/lxml/trunk/src/lxml/public-api.pxi

Here is their .pxd declaration:

http://codespeak.net/svn/lxml/trunk/src/lxml/etreepublic.pxd

And here is how you can cimport them from another module:

http://codespeak.net/lxml/capi.html#writing-external-modules-in-pyrex

That's all you need.

Stefan



More information about the Pyrex mailing list