1. SWIG
SWIG 允许向广泛的脚本语言公开 C/C++ 代码,包括 Ruby、Perl、Tcl 和 Pytxhon等。
SWIG (Simplified Wrapper and Interface Generator) 简单的包装和接口生成器
SWIG 可以 把C++代码封装成Python库
SWIG 实际上是一个编译器,获取C/C++的声明,用一个壳包起来,以便通过其他语言访问这些声明。
因此,SWIG 最大的好处就是将脚本语言的开发效率和 C/C++ 的运行效率结合起来。
2. C语言实例
2.1 编写 .h 和 .c 文件
.h 声明方法
int fact(int n);
.c 实现
#include "example.h"
int fact(int n) {
if (n < 0) {
return 0;
}
if (n == 0) {
return 1;
}
else {
return n * fact(n-1);
}
}
2.2 编写 .i 接口文件
%module example
%{
#define SWIG_FILE_WITH_INIT
#include "example.h"
%}
int fact(int n);
%module后面的名字是被封装的模块名称,Pyt