跟随官方教程
1. 编写C文件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); }
2. 编写接口文件 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();
3. 生成python接口文件——example.py、 example_wrap.c
swig -python example.i
4. 编译C文件
#这里官方教程没跑成功,自己find下路径
gcc -c example.c example_wrap.c -fpic -I/home/tools/Python-2.7.9/Include/ -I/home/tools/Python-2.7.9/
ld -shared example.o example_wrap.o -o _example.so
gcc -c example.c example_wrap.c -fpic -I/home/tools/Python-2.7.9/Include/ -I/home/tools/Python-2.7.9/
ld -shared example.o example_wrap.o -o _example.so
5. 运行
启动python(!!这里要使用和上面匹配的python版本)
>>> import example >>> example.fact(5) 120 >>> example.my_mod(7,3) 1
--end
相关推荐
标题中的“swig的学习以及国密的python封装”是指通过SWIG(Simplified Wrapper and Interface Generator)工具将中国的国家密码算法(简称国密)封装为Python库的过程。SWIG是一个工具,它允许C/C++代码与多种高级...
12. **Python与其他语言的交互**:Python可以通过多种方式与C/C++等其他语言集成,例如使用ctypes或SWIG。 13. **Python标准库**:标准库提供了许多实用的模块,如os、sys、time、datetime等,用于系统交互、进程...
**GDAL库学习笔记** GDAL(Geospatial Data Abstraction Library)是一个开源的地理空间数据处理库,广泛应用于地理信息系统(GIS)领域。它支持多种遥感图像、地图、栅格数据等地理数据格式,提供了丰富的接口供...
7. 粘性扩展:Python可以与其他语言编写的代码集成,利用Cython或SWIG等工具进行扩展。 8. 开源理念:Python是开放源代码的,拥有庞大的社区支持和丰富的库资源。 9. 库类丰富:Python有众多预封装的库,如NumPy、...
lmnn-gsoc2013-评论 该项目的GSoC 2013代码名为“实施度量学习算法及其在元基因... examples /:在C ++,Python和Octave中使用的示例。 此仓库中未包含在幕府时代将军进行的其他一些次要补充(例如LMNN的SWIG绑定)。