范例来自:http://www.iamlicky.cn/post/221.html
平台是linux,window下编译环节比较麻烦,所以放弃了。
a)首先编写你的c函数,比如如下创建一个test.c文件:
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- void
- func (int n )
- {
- printf("This is hello message came from C src!");
- printf("Let me show u something:/n");
- int i=0;
- for(i=1;i<n;i++){
- int j=1;
- for(j=1;j<i+1;j++)
- printf("%d*%d = %2d ", j, i, i*j);
- printf("/n");
- }
- printf("Did u enjoy it?/nBye,Bye, have fun @.@!/n");
- }
- char * get_time(){
- time_t ltime;
- time(<ime);
- return ctime(<ime);
- }
b)编写接口文件test.i:
- %module test
- %{
- /*Put header files here or function declarations like below*/
- extern void func(int n);
- extern char * get_time();
- %}
- extern void func(int n);
- extern char * get_time();
c)编译:
- #swig -python test.i (这里给个提示,如果是扩展c++,这里应该这样swig -c++ -python test.i)
- #gcc -c test.c test_wrap.c -I/usr/include/python2.6 (1》这里可能出现找不到Python.h,或者出现大量的定义神马错误,是缺少python-dev包,即包含各种头文件等的包,请goole:python-dev包 OR在此链接下载(注意版本),http://packages.debian.org/squeeze/python2.6-dev
1.2》我把dev包的内容覆盖好之后,又提示缺少pyconfig.h,如果没记错的话— —!,要把pyconfig-32.h改为pyconfig.h即可
2》提示gcc error,可能是你的linux木有gcc,请自行下载,扩展c++请下载多一个g++) - #ld -shared test.o test_wrap.o -o _test.so