Can't Extend
Mark Hammond
mhammond at skippinet.com.au
Sun May 14 20:08:32 EDT 2000
"Pete Shinners" <pshinners at mediaone.net> wrote in message
news:0MGT4.7890$WS3.61292 at typhoon.we.rr.com...
> i'm finding i cannot create my own C extensions for python.
> (this time checking with the FAQ) i'm stuck. using Visual C 6
> i am consistently getting this error.
>
> ImportError: dynamic module does not define init function (initincr)
>
>
> as far as i can see, this is incorrect. there is a simple initincr
> function. i'm assuming there is some sort of name mangling going on
> during the compile/link
The tool "dumpbin /exports incr.pyd" will dump the names of all exported
functions. If you function doesnt appear, you need to export it. It is
appears but mangled, then your guess is correct.
[Ahh - just looked lower and saw the code - unless you have a special
linker command-line option or a .def file, it is not exported - functions
are private in DLLs by default]
The simplest solution to either problem is to change your code to read:
extern "C" __delcspec(dllexport) initincr()
...
[Obviously with #ifdefs if it will be cross-platform]
Mark.
>
> anyone have a secret set of visual c flags that gets me a good
> clean extension PYD file? here is my sample code (if that helps)
>
> solutions welcome!
>
> -----------------------------------------
>
> #include<Python.h>
>
> static PyObject* incr_func(PyObject* self, PyObject* args);
> static PyObject* decr_func(PyObject* self, PyObject* args);
>
> static PyMethodDef IncrMethods[] =
> {
> {"incr", incr_func, METH_VARARGS},
> {"decr", decr_func, METH_VARARGS},
> {NULL, NULL}
> };
>
> void initincr()
> {
> Py_InitModule("incr", IncrMethods);
> }
>
>
> static PyObject* incr_func(PyObject* self, PyObject* args)
> {
> int val;
>
> if (!PyArg_ParseTuple(args, "i", &val))
> return NULL;
> ++val;
> return Py_BuildValue("i", val);
> }
>
>
> static PyObject* decr_func(PyObject* self, PyObject* args)
> {
> int val;
>
> if (!PyArg_ParseTuple(args, "i", &val))
> return NULL;
> --val;
> return Py_BuildValue("i", val);
> }
>
>
>
>
>
More information about the Python-list
mailing list