1. 程式人生 > >c++中嵌入python入門4 之 Boost.Python

c++中嵌入python入門4 之 Boost.Python

壞境python25 + vs2005 (2005真耗資源阿。。。)

有一段時間沒寫blog了。這幾天都在研究怎麼封裝c++,讓python可以用c++的庫。在網上發現了boost.python這個好咚咚。不

過在使用過程中碰到一點問題。本文教大家如何把

char const* greet()
{
   return "hello, world";
}

封裝成python。實際上這是python教程裡面的咚咚。


首先下載Boost,www.boost.org。boost.python在boost裡面了。在visual studio 2005 command prompt中navigation到

boost\boost_1_34_0\下。記得一定要用visual studio 2005 command prompt這個vs2005帶的tools,不要用cmd.exe,否則會

碰到很多錯誤的。然後就是把bjam.exe拷貝到一個能被找到的目錄下,或者直接也拷貝到boost\boost_1_34_0\下即可。然後,

設定python的根目錄和python的版本,也可直接把它們加到壞境目錄中,那樣就不用每次都設定一下。
set PYTHON_ROOT=c:/python25
set PYTHON_VERSION=2.5

接著就可以直接運行了,bjam -sTOOLS=vc-8_0
整個編譯過程要很長時間。。。

成功之後,就會有好多個boost_python-vc80-****.dll,.lib的,把他們都拷貝到一個能被系統找到的目錄,不妨直接把他們都

扔到c:\windows\system32下。

接著,我們開始編譯hello。navigation到boost\boost_1_34_0\libs\python\example\tutorial下,bjam -sTOOLS=vc-8_0執行

,在bin的目錄下即會生成hello.pyd。這下就基本成功了,如果沒成功的話,check一下上面boost_python的那些dll能否被系

統找到。另外,這裡有python25的一個bug。。。我花了很長時間才在python的mail lists中找到了。寒。。。

錯誤如下所示:
D:\Learn\Python\boost\boost_1_34_0\libs\python\example\tutorial>bjam
Jamroot:17: in modules.load
rule python-extension unknown in module Jamfile</D:/Learn/Python/boost/boost_1_3
4_0/libs/python/example/tutorial>.
D:/Learn/Python/boost/boost_1_34_0/tools/build/v2/build\project.jam:312: in load
-jamfile
D:/Learn/Python/boost/boost_1_34_0/tools/build/v2/build\project.jam:68: in load
D:/Learn/Python/boost/boost_1_34_0/tools/build/v2/build\project.jam:170: in proj
ect.find
D:/Learn/Python/boost/boost_1_34_0/tools/build/v2\build-system.jam:237: in load
D:\Learn\Python\boost\boost_1_34_0\libs\python\example\..\..\..\tools\build\v2/k
ernel\modules.jam:261: in import
D:\Learn\Python\boost\boost_1_34_0\libs\python\example\..\..\..\tools\build\v2/k
ernel/bootstrap.jam:132: in boost-build
D:\Learn\Python\boost\boost_1_34_0\libs\python\example\boost-build.jam:7: in mod
ule scope

解決辦法如下:
在boost\boost_1_34_0\tools\build\v2\目錄下找到user-config.jam檔案,開啟在
import toolset : using ;
下面加一行程式碼:
using python ;
再重新編譯一下boost,然後就沒問題了。tutorial裡面的hello能順利編譯通過。ps.這個問題困擾了我好長時間。。sigh。。

編譯成功後會產生一個hello.pyd,在bin的目錄下面。


有好多辦法測試此hello.pyd是否可以用。
方法一,把它拷貝到python25\dlls下,開啟IDLE,
>>> import hello
>>> hello.greet()
'hello, world'
>>> 
方法二,直接在當前目錄下寫一個python檔案,然後直接呼叫hello.pyd即可。總之,hello.pyd就是一個python檔案了。。嗯

。操作hello.pyd根其他python檔案是一樣的。
這樣就成功了。

如果碰到如下錯誤,是因為系統找不到boost_python的dll。強烈建議把他們都扔到system32下!。

>>> import hello

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import hello
ImportError: DLL load failed: 找不到指定的模組。
>>>


說明,hello.cpp在boost\boost_1_34_0\libs\python\example\tutorial目錄下。裡面的內容是:

//  Copyright Joel de Guzman 2002-2004. Distributed under the Boost
//  Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt 
//  or copy at http://www.boost.org/LICENSE_1_0.txt)
//  Hello World Example from the tutorial
//  [Joel de Guzman 10/9/2002]

char const* greet()
{
   return "hello, world";
}

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;

BOOST_PYTHON_MODULE(hello)
{
    def("greet", greet);
}


其中
BOOST_PYTHON_MODULE(hello)
{
    def("greet", greet);
}
是對greet從c++向python的一個封裝宣告吧,裝換就交給boost了。

 

先寫到這裡了。下次再寫。。嗯