C
和 C++
被公认为(理当如此)创建高性能代码的首选平台。对开发人员的一个常见要求是向脚本语言接口公开 C/C++
代码,这正是 Simplified Wrapper and Interface Generator (SWIG) 的用武之地。SWIG 允许您向广泛的脚本语言公开 C/C++
代码,包括 Ruby、Perl、Tcl 和 Python等。
为了建立python的扩展模块,SWIG采用分层的策略:用c写扩充模块,其余部分用python写。c包含低层次的封装,而python包含高层次的封装。分层策略是扩展模块的特定部分用特定的语言完成(而不全部用c/c++完成),另外通过利用2种语言,可以发挥各自语言的特性,增加灵活性。
1. 安装(Windows)
下载:http://www.swig.org/download.html
解压把swig.exe的地址写入到环境变量的环境变量的Path变量中。
2. 例子(c语言)
#2.1 用c语言编写头文件和源文件为
/* File: example.h */ int fact(int n);
/* File: example.c */ #include "example.h" int fact(int n) { if (n < 0){ /* This should probably return an error, but this is simpler */ return 0; } if (n == 0) { return 1; } else { /* testing for overflow would be a good idea here */ return n * fact(n-1); } }
#2.2 写swig模块写一个文件
/* File: example.i */ %module example %{ #define SWIG_FILE_WITH_INIT #include "example.h" %} int fact(int n);
#2.3 为了建python模块,利用-python参数执行swig
1
|
swig -python example.i
|
执行完命令后生成两个不同的文件:example_wrap.c和example.py。
自动生成文件名的原则:生成的c文件名与写的c文件名有关(例如写的c文件名为example.c则生成example_wrap.c);生成的python文件即.i文件中%module后面的名字。
#2.4 利用distutils生成动态库
python自带一个distutils工具,可以用它来创建python的扩展模块。使用它也很简单,只需要先定义一个配置文件,通常是命名为setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
""
"
setup.py
""
"
from distutils.core import setup, Extension
example_module = Extension(
'_example'
,
sources=[
'example_wrap.c'
,
'example.c'
],
)
setup (name =
'example'
,
version =
'0.1'
,
author =
"SWIG Docs"
,
description =
""
"Simple swig example from docs"
""
,
ext_modules = [example_module],
py_modules = [
"example"
],
)
|
注:头文件和源文件都是example.*,那么setup.py脚本中Extension的参数必须为“_example”
#2.5 编译
1
|
python setup.py build
|
会在本目录下build/lib*/下生成_example.pyd模块,可以直接使用,例如
1
2
3
4
|
>>>import example
>>>
print
example.fact(
4
)
24
>>>
|
可以吧动态模块直接生成当前目录下
1
|
python setup.py build_ext --inplace
|
3. 例子(c++)
和c一样,稍微区别
#3.1 用c语言编写头文件和源文件为
/* File: example.h */ int fact(int n);
/* File: example.cpp */
#include "example.h"
int fact(int n) {
if (n < 0){ /* This should probably return an error, but this is simpler */
return 0;
}
if (n == 0) {
return 1;
}
else { /* testing for overflow would be a good idea here */ return n * fact(n-1); } }
#3.2 写swig模块写一个文件
/* File: example.i */
%module example
%{
#define SWIG_FILE_WITH_INIT
#include "example.h"
%}
int fact(int n);
#3.3 为了建python模块,利用-python参数执行swig
1
|
swig -c++ -python example.i
|
执行完命令后生成两个不同的文件:example_wrap.cxx和example.py。
#3.4 利用distutils生成动态库
python自带一个distutils工具,可以用它来创建python的扩展模块。使用它也很简单,只需要先定义一个配置文件,通常是命名为setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
""
"
setup.py
""
"
from distutils.core import setup, Extension
example_module = Extension(
'_example'
,
sources=[
'example_wrap.cxx'
,
'example.cpp'
],
)
setup (name =
'example'
,
version =
'0.1'
,
author =
"SWIG Docs"
,
description =
""
"Simple swig example from docs"
""
,
ext_modules = [example_module],
py_modules = [
"example"
],
)
|
注:头文件和源文件都是example.*,那么setup.py脚本中Extension的参数必须为“_example”
#3.5 编译
1
|
python setup.py build_ext --inplace
|
转至:http://www.cnblogs.com/kaituorensheng/p/4464117.html