您的位置:首页 > 编程语言 > Python开发

swig c python

2014-02-19 17:02 316 查看
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'
>>>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: