让python和C/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
  • 下载gunmake,在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

  1. import os  
  2. import sipconfig  
  3.   
  4. # The name of the SIP build file generated by SIP and used by the build  
  5. # system.  
  6. build_file = "mymath.sbf"  
  7.   
  8. # Get the SIP configuration information.  
  9. config = sipconfig.Configuration()  
  10.   
  11. # Run SIP to generate the code.  
  12. cmd = " ".join([config.sip_bin, "-c", ".", "-b", build_file, "mymath.sip"])  
  13. os.system(cmd)  
  14.   
  15. # Create the Makefile.  
  16. makefile = sipconfig.SIPModuleMakefile(config, build_file)  
  17.   
  18. # Add the library we are wrapping.  The name doesn't include any platform  
  19. # specific prefixes or extensions (e.g. the "lib" prefix on UNIX, or the  
  20. # ".dll" extension on Windows).  
  21. makefile.extra_libs = ["mymath"]  
  22. makefile.LIBDIR.append("./")  
  23.   
  24. # Generate the Makefile itself.  
  25. makefile.generate()  

mymath.h

  1. /* 
  2. define my own math library 
  3. */  
  4.   
  5. class MyMath{  
  6.   
  7. public:  
  8.     int Add(int a, int b);  
  9.     int Minus(int a, int b);  
  10. };  

mymath.cpp

  1. #include "mymath.h"  
  2.   
  3. int MyMath::Add(int a, int b)  
  4. {  
  5.     return a + b;  
  6. }  
  7.   
  8. int MyMath::Minus(int a, int b)  
  9. {  
  10.     return a - b;  
  11. }  

mymath.sip

  1. // Define the SIP wrapper to the word library.  
  2.   
  3. %Module MyMath  
  4.   
  5. class MyMath {  
  6.   
  7. %TypeHeaderCode  
  8. #include "mymath.h"  
  9. %End  
  10.   
  11. public:  
  12.     int Add(int a, int b);  
  13.     int Minus(int a, int b);  
  14. };  

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

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

可以看看makefile文件的内容。

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

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

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

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

>>> import MyMath
>>> 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。具体内容请详看相应的参考文件。

-----------------打造高质量的文章 更多关注 把酒泯恩仇---------------

为了打造高质量的文章,请  推荐  一个吧。。。。谢谢了,我会写更多的好文章的。

请关注sina微博:http://weibo.com/baiyang26

把酒泯恩仇官方博客:http://www.ibaiyang.org 【推荐用google reader订阅】

把酒泯恩仇官方豆瓣:http://www.douban.com/people/baiyang26/

原文地址:https://www.cnblogs.com/chengjian-physique/p/8180011.html