469,609 Members | 1,082 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

SWIG Python undefined reference

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'])
])
Jun 27 '08 #1
3 3230

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
Jun 27 '08 #2
On Apr 29, 11:31 am, Soren <soren.skou.niel...@gmail.comwrote:
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
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
Jun 27 '08 #3
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

Soren wrote:
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'])
])
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Uwe Mayer | last post: by
2 posts views Thread by superprad | last post: by
reply views Thread by Jerry He | last post: by
reply views Thread by Jerry He | last post: by
1 post views Thread by superprad | last post: by
2 posts views Thread by ajikoe | last post: by
reply views Thread by Michael Yanowitz | last post: by
2 posts views Thread by mk | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.