在python中使用c语言扩展模块的简单示例
2011-07-01 16:17
1171 查看
我是在windows中安装了EPD的python安装包,然后看了一下swig的官方文档试了一下,发现很方便
一、假设有如下c文件
二、现在想在python中使用它,可以很容易使用swig来编写c语言扩展模块
编写如下的swig接口文件example.i ,和example.c保存在同一目录下
三、编写如下的DOS批处理文件,命名为BuilderInstall.bat,也放在同一目录里面
然后运行(双击该文件即可)BuilderInstall.bat,完成!
四、测试模块。下面可以再python中使用它了:
一、假设有如下c文件
/* 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); }
二、现在想在python中使用它,可以很容易使用swig来编写c语言扩展模块
编写如下的swig接口文件example.i ,和example.c保存在同一目录下
/* 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();
三、编写如下的DOS批处理文件,命名为BuilderInstall.bat,也放在同一目录里面
swig -python example.i gcc -shared example.c example_wrap.c -IC:/python27/include -LC:/python27/libs -lpython27 -o _example.pyd xcopy *.py* C:/Python27/Lib/site-packages/
然后运行(双击该文件即可)BuilderInstall.bat,完成!
四、测试模块。下面可以再python中使用它了:
>>>import _example #注意使用swig编写的接口模块前面都有下划线_ >>>_example.My_variable 3.0 >>>_example.fact(5) 120 >>>_example.get_time() 'Fri Jul 01 16:12:20 2011' >>>
相关文章推荐
- Python扩展模块——调用WindowsAPI(pywin32的简单使用)
- 【Python】使用C语言来扩展Python模块
- Python中使用urllib2模块编写爬虫的简单上手示例
- Python中使用urllib2模块编写爬虫的简单上手示例
- 使用c语言来扩展python模块
- 使用C语言扩展Python程序的简单入门指引
- 使用C语言扩展Python
- python使用os模块的os.walk遍历文件夹示例
- [Python学习] Logging模块的简单使用
- 使用C语言扩展Python(二)
- 使用C语言扩展Python(四)
- 使用Visual Studio.Net 2005开发Python扩展模块
- Python中的数据对象持久化存储模块pickle的使用示例
- 重温python之OS模块的简单使用5:os.chown()更改文件所有者
- Python使用jsonpath-rw模块处理Json对象操作示例
- Python中operator模块的操作符使用示例总结
- Python:简单select模块使用
- 重温python之OS模块的简单使用9:os.fchdir(), os.fchmod(), os.fchown()
- Python 正则表达式的简单使用示例
- python小程序----queue模块的简单使用