讓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版本的編譯器。
然後執行
makemake install
大功告成。
第三步:讓C/C++跑起來吧
建立3個檔案。
configure.py mymath.cpp mymath.h mymath.sip
內容分別是:
configure.py
view plaincopy to clipboardprint?
- 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
view plaincopy to clipboardprint?
- /*
- define my own math library
- */
-
- class MyMath{
-
- public:
- int Add(int a, int b);
- int Minus(int a, int b);
- };
mymath.cpp
view plaincopy to clipboardprint?
- #include "mymath.h"
-
- int MyMath::Add(int a, int b)
- {
- return a + b;
- }
-
- int MyMath::Minus(int a, int b)
- {
- return a - b;
- }
mymath.sip
view plaincopy to clipboardprint?
- // Define the SIP wrapper to the word library.
-
- %Module MyMath
-
- class MyMath {
-
- %TypeHeaderCode
- #include "mymath.h"
- %End
-
- public:
- int Add(int a, int b);
- int Minus(int a, int b);
- };
執行檔案下的configure.py,你可以看到產生以下檔案:
mymath.sbf sipAPIMyMath.h sipMyMathcmodule.cpp sipMyMathMyMath.cpp Makefile
可以看看makefile檔案的內容。
在這裡需要產生mymath的靜態連結庫,用以下命令:
g++ -c mymath.cpp ----->產生objective file,mymath.oar 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__', '__subclasshook__', '__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/