http://www.swig.org/translations/chinese/tutorial.html
/* File : 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(<ime); return ctime(<ime); }
/* 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();
swig -python example.i
xsheng@bcxcp-AS-dev01:~/workspace/swig$ gcc -fpic -c example.c example_wrap.c -I /home/ezhao/tools/python-2.7/include/python2.7/
xsheng@bcxcp-AS-dev01:~/workspace/swig$ ld -shared example.o example_wrap.o -o _example.so
现在可以使用如下Python模块 :
>>> import example
>>> example.fact(5)
120
>>> example.my_mod(7,3)
1
>>> example.get_time()
'Sun Feb 11 23:01:07 1996'
>>>