您的位置:首页 > 编程语言 > C语言/C++

使用SWIG将C++绑定到python

2012-07-01 16:45 597 查看
一、为什么要绑定

c++和python都是通用的编程语言,能完成网络、界面、数据库、计算、商务、游戏等任意的应用,但是相比较而言,python比c++更高级一些。python是个解释语言,C++语言需要编译,解决一个相同的问题,pyhon比C++节省5倍以上的代码量,有人认为一个python程序员2个月能完成的任务,两个C++程序样一年也完成不了,python还有许多比较好的特性,比如动态数据类型定义、不需要指针、模块、宏预处理、不需要明确的分配内存,所以python应用比较普遍,在大多数情况下,python还算是快的,但对于快有更高要求的情况下,c++就更能满足要求,简而言之,python比C++用起来跟方便,但是C++运行起来更快,所以需用C++编写需要大运算量的场合,然后供python调用。也就是把c++绑定到python里。

二,什么是绑定

简而言之,在一个语言里可以调用另一个语言的功能,就叫绑定。

三,什么是SWIG

SWIG就是simplified wrapper interface generator也就是简化的绑定接口生成器。

SWIG可以提供多个语言之间的绑定接口生成,但是本文只讲C++与python之间的绑定生成问题。

四、如何实现

SWIG如何安装之类的就略去了。

最好的例子就是直接上SWIG的网站
http://www.swig.org/tutorial.html
五、需要注意的几点

如下几点说明主要是针对的是使用windows,并用Visual studio进行程序开发的用户

1、根据swig的文档的说明把PYTHON_INCLUDE和PYTHON_LIB两个环境变量设置好

2、用visual studio创建一个dll的项目

3、生成.i文件并对其进行属性设置

4、对项目属性进行设置

具体可参见下面的说明:

How to use SWIG under VS2010.

lets define you want to obtain mylib.py, so you created some SWIG interface files with mylib.i as "main" file. I assume that you already have a solution with project with your C++ classes there.

(1) First create C++ project for SWIG interface. Use Visual C++->Class library project which should create a C++ DLL stub. I put all .i files there. And set visual studio to highlight .i as .h - it is handy.

(1.1) Add mylib_wrap.cxx file to the project (create empty file while swig hasn't generated one yet)

(2)

a) Press right button over the mylib.i, choose properties.

b) set ItemType as "Custom build tool".

In custom build step window:

c) Command line field should be something like:

echo In order to function correctly, please ensure the following environment variables are
correctly set:
echo PYTHON_INCLUDE: %PYTHON_INCLUDE%
echo PYTHON_LIB: %PYTHON_LIB%
echo on
C:\swig\swig.exe -c++ -python %(FullPath)

change C:\swig\swig.exe to your path to SWIG

d) In Outputs field:

$(InputName)_wrap.cxx




(3) Go to this project properties:

a) C++ tab -> Additional Include Directories

add $(PYTHON_INCLUDE); ...

c) Linker -> Output File
Path-You-Needed\_mylib.pyd

d) Linker -> Enable Incremental Linking

set as No (/INCREMENTAL:NO)

e) Linker -> Input -> Additional Dependencies

add $(PYTHON_LIB);...

f) C/C++ -> Precompiled Headers:Switch off precompiled headers, set *Not Using Precompiled Headers and remove stdafx files after

g) General tab. Just check that these are set:

Configuration type = Dynamic Library (.dll)

Character set = Use Unicode Character Set

Common Language Runtime Support = No Common Language Runtime Support

It compiles.

这个说的够详细了,参照这个来就可以搞定

需要注意的问题:

在debug下面编译的时候会报错,报的错误是没有找到python27_d.lib

这个问题的解决方法:

在python的安装目录下找到pyconfig.h

1、把其中有关python27_d.lib用python27.lib代替

2、把

#ifdef _DEBUG

# define Py_DEBUG

#endif

修改为

#ifdef _DEBUG

//# define Py_DEBUG

#endif

记得要把生成的_wrap.cxx文件加进去

swig神器还可以将很多C与C++的特性弄到python里去,使得在python里也可以象操作C与C++一样

例如:

如果要运用int型数组:

则可在.i文件里添加如下一句:

%include "carrays.i"

%array_class(int, intArray);

如果生成的python文件是example.py

则可以象如下操作:

import example

a=example.intArray(10)

for i in range(10):

     a[i]=i

这对应的操作就是:

int a[10];

for(int i=0;i<10;i++)

{

a[i]=i;

}

内联方式编程swig也是支持的,但是要记住把_wrap.cxx这个文件选到项目里面去
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: