Interfacing python and C

steve William steve.william at
Fri Mar 27 18:28:44 CET 2009

Hi All,

I'm using SWIG for the first time and I am facing some problems with user
defined header files. I'm trying to use my own header file in a C program
which would be interfaced with python.

The header file is test.h:
*#include <stdio.h>

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

The C program is test1.c:
*#include <stdio.h>
#include "test.h"

int calc_fact(int a)
    return (fact(a));

The interface file is test1.i:
*%module test1

#include "stdio.h"
#include "test.h"

int calc_fact(int a);*

The commands that I used to generate the wrappers are:
*swig -python test1.i
gcc -c test1.c test1_wrap.c -I/usr/include/python2.5
g++ -shared test1_wrap.o -o*

When I try to import test1, I get an error saying:
*>>> import test1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "", line 22, in <module>
    import _test1
ImportError: ./ undefined symbol: calc_fact*

I'm not sure why this is happening because when I try without user defined
header file, it works. Also the error says that it does not recognize *
calc_fact* which is a function that I want to access from python and is
declared in the interface file.

Is there any specific way in which user defined headers need to be declared
in the interface file? Should the user defined header be placed in the
/usr/include  directory?

Any help on this is highly appreciated.

Thank you,
Steve James William.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list