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

mymath.h

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

mymath.cpp

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

mymath.sip

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

执行文件下的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/

最新文章

  1. 高性能IO模型浅析
  2. My Baits入门(一)mybaits环境搭建
  3. Git典型使用场景
  4. 史上最全github使用方法:github入门到精通
  5. u-boot 流程分析
  6. Intelligencia.UrlRewriter在IIS 7.0下的完全配置攻略
  7. 瓶颈生成树与最小生成树 POJ 2395 Out of Hay
  8. 运行时(iOS)
  9. Debug调试
  10. h5的api dom全屏展示
  11. ListView列表项
  12. 如何在MySQL中设置外键约束以及外键的作用
  13. 团队作业4——第一次项目冲刺(Alpha版本)2017.4.25
  14. VS2005 添加lib 的方法
  15. 关于win8/win8.1系统不能调节亮度的解决办法
  16. 大量Python开源第三方库资源分类整理,含菜鸟教程章节级别链接
  17. osg shader 相机观察矩阵逆矩阵 求顶点世界坐标
  18. [转帖]紫光展锐5G芯片
  19. Ansible Role
  20. 【转】【Python】python使用urlopen/urlretrieve下载文件时出现403 forbidden的解决方法

热门文章

  1. vue2.0中ckeckbox(复选框)的使用心得,及对click事件和change的理解
  2. SpringBoot之自动配置原理
  3. python 使用uuid 出现重复
  4. js字符串去掉所有空格
  5. Python全栈学习:匿名函数使用规范
  6. 适合学习的QT开源项目-SerialTool
  7. makefile学习(1)
  8. vim 命令总结
  9. Neural Network
  10. Django补充知识点——用户管理