multiple modules from single c extension

Eric Frederich eric.frederich at gmail.com
Wed Dec 1 17:12:07 EST 2010


I have an extension to some C library that I created using the guide
found here...

    http://docs.python.org/extending/extending.html

I am starting to have A LOT of functions being wrapped.

The library that I'm creating bindings for is organized into modules.
In fact, all of their function calls start with a prefix like
ABC_do_something, XYZ_something_else.

I'd like to start putting the bindings for each module into a separate
C file and have each set of bindings end up in its own Python module
as well.

Is this possible to do using a single .dll / .pyd file so that I can
use a single Visual Studio project for these bindings?

Is it just a matter of initializing more than one module in the
initspam function?

    PyMODINIT_FUNC
    initspam(void)
    {
        (void) Py_InitModule("spam", SpamMethods);
        // can I do this?
        (void) Py_InitModule("spam.ABC", SpamABCMethods);
    }

Would this even work?
How would I get a hold of SpamABCMethods now that its in a different file?
I tried creating a .h file for the ABC functions which had the
prototypes defined as well as the static PyMethodDef SpamABCMethods[]
array.
This way I could include SpamABC.h which had the array.
When I try doing that though, I get link errors saying things like
functions are declared but not defined.
The example online has everything in a single C file.
If I split things up into a .h and a .c file... what should be
declared as staic?, should anything be declared as extern?
This is where I'm stuck.

Thanks,
~Eric



More information about the Python-list mailing list