compiling extensions on linux

Joe Connellan joec at mill.co.uk
Mon Sep 2 12:52:00 EDT 2002


it does have a initmyextension declared - Here's a simple example of my
problem - I've taken all c++ related stuff out so I can use gcc

#include "Python.h"
#include<stdio.h>

int
converter(void *from, unsigned long *to)
{
        to = (unsigned long*)from;
        printf("%d %d %d", to[0], to[1], to[2]);
        return 1;
}

static PyObject *
myextension_printHi(PyObject *self, PyObject *args)
{
        printf("Hi\n");
        Py_INCREF(Py_None);
        return Py_BuildValue("");
}

static PyMethodDef myextension_methods[] = {
        {"printHi", myextension_printHi, 1, "printHi() doc string"},
        {NULL, NULL}
};

void
initmyextension(void)
{
        Py_InitModule("myextension", myextension_methods);
}


$ gcc -shared -o myextension.so myextension.cpp
-I/usr/local/dist/Python-2.2.1/Include/ -I/usr/include/python2.2/

Python 2.2 (#1, Apr 12 2002, 15:29:57)
[GCC 2.96 20000731 (Red Hat Linux 7.2 2.96-109)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import myextension
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ImportError: dynamic module does not define init function (initmyextension)
>>>


Thanks for the help

Joe

"Martin v. Löwis" wrote:

> Joe Connellan <joec at mill.co.uk> writes:
>
> > I'm trying to port an extension of mine from windows to linux and am not
> > sure how to include the myExtension.def file in the compile
>
> You shouldn't have a .def file in the first place - I recommend that
> you use distutils instead.
>
> > eg if I use
> >
> > g++ -shared -o myextension.so
> >
> > it compiles fine but I get the following error when importing it into
> > python
> >
> > >>> import myextension
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in ?
> > ImportError: dynamic module does not define init function
> > (initmyextension)
>
> Nothing to this respect is needed on Linux. Are you sure your module
> does define a initmyextension function?
>
> It might be that it defines initmyextension__Fv instead, since you
> compile your extension as a C++ program.
>
> You should compile it as a C program, unless it really is a C++
> program, in which case you need to define the init function as extern
> "C".
>
> HTH,
> Martin




More information about the Python-list mailing list