SWIG Python undefined reference

2546 views
Skip to first unread message

Soren

unread,
Apr 29, 2008, 5:07:04 AM4/29/08
to
Hi,

I went through the SWIG tutorial for the example named "simple".

I managed to get to the first step, creating example_wrap.c using
swig, and doing:
"gcc -fpic -c example_wrap.c -IC:\python24\include " to create
example_wrap.o

But when I needed to compile the library file using:
"gcc -shared example_wrap.o -o examplemodule.so" I received a lot of
undefined reference compiler errors like:

example_wrap.o(.text+0x35a5):example_wrap.c: undefined reference to
`_imp__PyErr
_SetString'

there are many other similar errors all prefaced with _imp__Py, so I
am assuming there is a linker error with the python libraries. I have
adjusted my PATH variable to include all the python directories (libs/
dlls).

Does anyone here have any suggestions?

FILES FROM TUTORIAL:


//example.c
#include <time.h>
double My_variable = 3.0;

int fact(int n) {
if (n <= 1) return 1;
else return n*fact(n-1);
}

int my_mod(int x, int y) {
return (x%y);
}

char *get_time()
{
time_t ltime;
time(&ltime);
return ctime(&ltime);
}
//***************************************************************

//example.i
%module example
%{
/* Put header files here or function declarations like below */
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
%}

extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
//***************************************************************

//setup.py
from distutils.core import setup, Extension

setup(name='example',
version = '1.0',
ext_modules=[
Extension('example', ['example.c', 'example.i'])
])

Soren

unread,
Apr 29, 2008, 5:31:09 AM4/29/08
to

Ok I found out how to do it using:

gcc -Ic:\python24\include -Lc:\python24\libs --shared example_wrap.c
example.c -lpython24 -o _example.pyd

but now I get a "dynamic module does not define init function" error
when I try to import it into python..

Anyone??

Soren

Soren

unread,
Apr 29, 2008, 6:45:27 AM4/29/08
to

In case anyone is having the same problem the solution for me was:

gcc example.c example_wrap.c -Ic:\python24\include -Lc:\python24\libs -
lpython24 -Xlinker -expoert-dynamic -shared -o _example.pyd

Soren

Paul Melis

unread,
Apr 29, 2008, 7:18:32 AM4/29/08
to
Instead of manually trying to get all the options to gcc correct you
might want to look at using distutils for compiling your extension.
See the SWIG documentation, section 30.2.2
(http://www.swig.org/Doc1.3/Python.html#Python_nn6)

Paul

Reply all
Reply to author
Forward
0 new messages