FAQ
Hi,

I've just starting out embedding Python in C and get the following error
when I try and import a module that in turn imports the math module.

ImportError: /usr/lib/python2.4/lib-dynload/math.so: undefined symbol:
PyExc_FloatingPointError

The module is as follows:

# begin script.py
import math

def fn(i):
print "i is %d" % i

# end script.py

and the C code is :

// start main.c
#include <Python.h>

int main(int argc, char *argv[]) {
PyObject *mymod, *fn, *strargs;

Py_Initialize();

mymod = PyImport_ImportModule("script");

if(mymod == NULL) {
PyErr_Print();
exit(1);
}

fn = PyObject_GetAttrString(mymod, "fn");

if(fn == NULL) {
PyErr_Print();
exit(1) ;
}

strargs = Py_BuildValue("(i)", 0);
PyEval_CallObject(fn, strargs);

Py_Finalize();
return 0;
}
// end main.c

Testing script.py by running python and importing the module works fine.
Commenting out the import math statement and import other modules (sys,
string etc) work fine.

The C program is being built like so:

gcc main.c -c -I-I/usr/include -I/usr/include -I/usr/include/python2.4
-I/usr/include/python2.4 -DNDEBUG -g -O3 -Wall -Wstrict-prototypes
gcc main.o -L/usr/lib -lpthread -ldl -lutil
-lm /usr/lib/python2.4/config/libpython2.4.a -o main

I've tried the above on two machines, one Debian stable and the other
Debian testing. Same results on both.

It's like I'm missing a library or something, any ideas ?

Cheers,

Simon Newton

Search Discussions

  • Martin v. Löwis at Aug 17, 2005 at 7:52 am

    Simon Newton wrote:
    gcc main.c -c -I-I/usr/include -I/usr/include -I/usr/include/python2.4
    -I/usr/include/python2.4 -DNDEBUG -g -O3 -Wall -Wstrict-prototypes
    gcc main.o -L/usr/lib -lpthread -ldl -lutil
    -lm /usr/lib/python2.4/config/libpython2.4.a -o main

    I've tried the above on two machines, one Debian stable and the other
    Debian testing. Same results on both.

    It's like I'm missing a library or something, any ideas ?
    No. You need to export the Python symbols from your executable to
    extension modules. IOW, you need to pass

    -Xlinker -export-dynamic

    to the gcc invocation.

    Regards,
    Martin
  • Simon Newton at Aug 17, 2005 at 9:53 am

    On Wed, 2005-08-17 at 09:52 +0200, "Martin v. L?wis" wrote:
    Simon Newton wrote:
    gcc main.c -c -I-I/usr/include -I/usr/include -I/usr/include/python2.4
    -I/usr/include/python2.4 -DNDEBUG -g -O3 -Wall -Wstrict-prototypes
    gcc main.o -L/usr/lib -lpthread -ldl -lutil
    -lm /usr/lib/python2.4/config/libpython2.4.a -o main
    No. You need to export the Python symbols from your executable to
    extension modules. IOW, you need to pass

    -Xlinker -export-dynamic

    to the gcc invocation.
    Thanks Martin,

    -export-dynamic fixed it.

    Simon
  • En Karpachov at Aug 17, 2005 at 10:16 am

    On Wed, 17 Aug 2005 14:29:43 +0800 Simon Newton wrote:

    The C program is being built like so:

    gcc main.c -c -I-I/usr/include -I/usr/include -I/usr/include/python2.4
    -I/usr/include/python2.4 -DNDEBUG -g -O3 -Wall -Wstrict-prototypes
    gcc main.o -L/usr/lib -lpthread -ldl -lutil
    -lm /usr/lib/python2.4/config/libpython2.4.a -o main
    Try to link libpython as a library (as it should be), not as an object
    module:

    gcc main.o -L/usr/lib -lpthread -ldl -lutil -lm -lpython2.4 -o main

    --
    jk

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 17, '05 at 6:29a
activeAug 17, '05 at 10:16a
posts4
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase