#python和C/C++#让python和C/C++联姻

转载:http://www.ibaiyang.org/2012/12/04/generating-python-bindings-for-c-and-c-libraries/

python-c++

让python不在孤单,轻易而举的为python写C/C++第三方库。

我们都知道python很慢,特别是处理大数据的时候,简直慢到极致,如果在知道代码的瓶颈的时候,可以将需要大量计算的数据块放在C/C++代码里运算,然后再将数据返回给python。对,这也让python易于扩展,这样我们可以将大部分时间放在核心的代码上。

在看过一些复杂的调用方式之后,总觉得上手不易,麻烦,细想,这完全与python的simple is better哲理违背啊,果断放弃。这几天在深圳先进研究院做项目的时候,用到了QT,然后无意中发现了SIP。官方解释是SIP is a tool for automatically generating Python bindings for C and C++ libraries(还是用原文比较好,翻译之后总觉得变味了)。起初,SIP是为了PyQt而建,不过也能为其他C/C++库生成python的代码。

就此,发现了让python调用C/C++代码的利器。这样我们就可以游刃有余的穿梭在python和C/C++代码之间了。It’s perfect。有时细想,python开发的高速度加上C/C++运行的高速度,能让JAVA汗颜了吧。不过,企业承认JAVA的稳定性还是NO.1的,我也不敢乱加评价。

那好,我们开始SIP之旅吧。

下面我是在windows下的配置,不过我没有用windows下默认的编译器,因为我发现中途遇见了各种问题,彻底让我心碎了,所以我用了mingw32.

第一步:配置环境

  • 下载mingw32
  • 下载SIP
  • 下载gnumake,在windows下运行make还需要libintl, libiconv.
最重要的是,分别将mingw32,make等bin目录放在PATH下,否则找不到make,g++等命令。

第二步:安装SIP

切换到SIP的根目录下,运行

python configure.py --platform win32-g++

指定SIP在C/C++代码时,是使用mingw32版本的编译器。

然后执行

make

make install

大功告成。

第三步:让C/C++跑起来吧

创建3个文件。

configure.py   mymath.cpp   mymath.h   mymath.sip

内容分别是:
configure.py

import os
import sipconfig

# The name of the SIP build file generated by SIP and used by the build# system.
build_file ="mymath.sbf"# Get the SIP configuration information.
config = sipconfig.Configuration()# Run SIP to generate the code.
cmd =" ".join([config.sip_bin,"-c",".","-b", build_file,"mymath.sip"])
os.system(cmd)# Create the Makefile.
makefile = sipconfig.SIPModuleMakefile(config, build_file)# Add the library we are wrapping.  The name doesn't include any platform# specific prefixes or extensions (e.g. the "lib" prefix on UNIX, or the# ".dll" extension on Windows).
makefile.extra_libs =["mymath"]
makefile.LIBDIR.append("./")# Generate the Makefile itself.
makefile.generate()

mymath.h

/*
define my own math library
*/classMyMath{public:intAdd(int a,int b);intMinus(int a,int b);};

mymath.cpp

#include"mymath.h"intMyMath::Add(int a,int b){return a + b;}intMyMath::Minus(int a,int b){return a - b;}

mymath.sip

// Define the SIP wrapper to the word library.%ModuleMyMathclassMyMath{%TypeHeaderCode#include"mymath.h"%Endpublic:intAdd(int a,int b);intMinus(int a,int b);};

执行文件下的configure.py,你可以看到生成以下文件:

mymath.sbf  sipAPIMyMath.h   sipMyMathcmodule.cpp  sipMyMathMyMath.cpp  Makefile

可以看看makefile文件的内容。

在这里需要生成mymath的静态链接库,用以下命令:

g++-c mymath.cpp  ----->生成objective filemymath.o
ar cr mymath.lib mymath.o   ----->生成静态链接库mymath.lib

到此,离成功还有半步距离了。
然后再执行make,即生成MyMath.pyd,如果你想安装MyMath.pyd,则make install即可。

我们来看看我们扩展的C/C++库吧。

>>>importMyMath>>> dir(MyMath)['MyMath','__doc__','__file__','__name__','__package__']>>> s =MyMath.MyMath()>>> dir(s)['Add','Minus','__class__','__delattr__','__dict__','__doc__','__format__','__getattribute__','__hash__','__init__','__module__','__new__','__reduce
__','__reduce_ex__','__repr__','__setattr__','__sizeof__','__str__','__sub
classhook__','__weakref__']>>> s.Add(1,2)3

好,白杨到此一游。Enjoy it。具体内容请详看相应的参考文件。

原文地址:https://www.cnblogs.com/ldjhust/p/3448768.html