Interfacing python and C

steve William steve.william at gmail.com
Fri Mar 27 13:28:44 EDT 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
-I/usr/lib/python2.5/config
g++ -shared test1_wrap.o -o _test1.so*

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 "test1.py", line 22, in <module>
    import _test1
ImportError: ./_test1.so: 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: <http://mail.python.org/pipermail/python-list/attachments/20090327/017d7768/attachment-0001.html>


More information about the Python-list mailing list