使用SWIG将C++绑定到python
2012-12-30 17:17
633 查看
原文地址:http://blog.csdn.net/hugewave/article/details/7705392
一、为什么要绑定
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:
change C:\swig\swig.exe to your path to SWIG
d) In Outputs field:
(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.
这个说的够详细了,参照这个来就可以搞定
一、为什么要绑定
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.
这个说的够详细了,参照这个来就可以搞定
相关文章推荐
- 使用SWIG将C++绑定到python
- Windows下python使用SWIG调用C++ dll
- 使用SWIG实现C++导出python接口的配置
- centos中使用swig实现python调用c++
- linux下使用swig让python调用C++(复杂版:包括多文件调用和链接库)
- 使用swig实现C++的python扩展功能
- linux下使用swig让python调用C++(复杂版:包括多文件调用和链接库)
- 使用SWIG实现Python调用C/C++代码
- Python调用C++函数(SWIG,VS2013使用numpy.i完成Numpy与C++数组转换)
- 用SWIG来生成Qt C++动态库的Python绑定
- 使用SWIG和Python对C/C++进行单元测试(二)
- 使用swig在python中调用c++代码
- 使用swig进行C/C++对Python的扩展
- 使用Shiboken为C++和Qt库创建Python绑定
- linux下使用SWIG实现Python调用C++ sdk读取相机图片
- centos7下使用swig扩展python接口来调用c++ 实现声纹识别
- 使用SWIG和Python对C/C++进行单元测试(一)
- Linux下利用SWIG完成Python对使用OpenCV的C++文件的调用
- 使用SWIG Python动态绑定C++对象
- python ctypes 探究 ---- python 与 c 的交互 近几天使用 python 与 c/c++ 程序交互,网上有推荐swig但效果都不理想,所以琢磨琢磨了 python 的 ct