Wrapping C with Python

Anish Chapagain anishchapagain at gmail.com
Mon Aug 4 13:12:03 CEST 2008


Hi!!
I tried wrapping a simple C code suing SWIG to Python, but am having
problem,
my .c file is,

Step 1:
example.c
--------------
double val=3.0;
int fact(int n)
{
if(n<=1)
return 1;
else
return n*fact(n-1);
}

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

Step 2:
I then created interface file as.
example.i
--------------
%module example
%{
%}
extern int fact(int n);
extern int mod(int x,int y);

Step 3:
I compiled the .i file with SWIG,
                                           c:\python25\swig> swig -
python example.i
It creates example.py and example_wrap.c

Step 4:
I compiled example.c and example_wrap.c individually
 c:\python25\mingw\bin>   gcc -c example.c                  (it create
example.o)
 c:\python25\mingw\bin>   gcc -c example_wrap.c -Ic:
\python25\include    (it create example_wrap.o)

Step 5:
building .pyd for windows,
 c:\python25\mingw\bin>   gcc -shared *.o -o _example.pyd -Lc:
\python25\libs -lpython25 (it creates _example.pyd)


Then, i imported example module by palcing it in python25 directory,
it get's imported but i'm not able to use function, when i called
                         >>>print example.fact(4)
                         >>>print example.mod(4,2)
It is nither showing any error nor message...
even i tried simple void fact() by printf inside .c file and is not
showing ...

hope for help...
regard's
Anish



More information about the Python-list mailing list