壞境python25 + vs2005 (2005真耗資源阿。。。)
有一段時(shí)間沒(méi)寫blog了。這幾天都在研究怎么封裝c++,讓python可以用c++的庫(kù)。在網(wǎng)上發(fā)現(xiàn)了boost.python這個(gè)好咚咚。不
過(guò)在使用過(guò)程中碰到一點(diǎn)問(wèn)題。本文教大家如何把
char const* greet()
{
return "hello, world";
}
封裝成python。實(shí)際上這是python教程里面的咚咚。
首先下載Boost,www.boost.org。boost.python在boost里面了。在visual studio 2005 command prompt中navigation到
boost\boost_1_34_0\下。記得一定要用visual studio 2005 command prompt這個(gè)vs2005帶的tools,不要用cmd.exe,否則會(huì)
碰到很多錯(cuò)誤的。然后就是把bjam.exe拷貝到一個(gè)能被找到的目錄下,或者直接也拷貝到boost\boost_1_34_0\下即可。然后,
設(shè)置python的根目錄和python的版本,也可直接把它們加到壞境目錄中,那樣就不用每次都設(shè)置一下。
set PYTHON_ROOT=c:/python25
set PYTHON_VERSION=2.5
接著就可以直接運(yùn)行了,bjam -sTOOLS=vc-8_0
整個(gè)編譯過(guò)程要很長(zhǎng)時(shí)間。。。
成功之后,就會(huì)有好多個(gè)boost_python-vc80-****.dll,.lib的,把他們都拷貝到一個(gè)能被系統(tǒng)找到的目錄,不妨直接把他們都
扔到c:\windows\system32下。
接著,我們開(kāi)始編譯hello。navigation到boost\boost_1_34_0\libs\python\example\tutorial下,bjam -sTOOLS=vc-8_0運(yùn)行
,在bin的目錄下即會(huì)生成hello.pyd。這下就基本成功了,如果沒(méi)成功的話,check一下上面boost_python的那些dll能否被系
統(tǒng)找到。另外,這里有python25的一個(gè)bug。。。我花了很長(zhǎng)時(shí)間才在python的mail lists中找到了。寒。。。
錯(cuò)誤如下所示:
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文件,打開(kāi)在
import toolset : using ;
下面加一行代碼:
using python ;
再重新編譯一下boost,然后就沒(méi)問(wèn)題了。tutorial里面的hello能順利編譯通過(guò)。ps.這個(gè)問(wèn)題困擾了我好長(zhǎng)時(shí)間。。sigh。。
。
編譯成功后會(huì)產(chǎn)生一個(gè)hello.pyd,在bin的目錄下面。
有好多辦法測(cè)試此hello.pyd是否可以用。
方法一,把它拷貝到python25\dlls下,打開(kāi)IDLE,
>>> import hello
>>> hello.greet()
'hello, world'
>>>
方法二,直接在當(dāng)前目錄下寫一個(gè)python文件,然后直接調(diào)用hello.pyd即可。總之,hello.pyd就是一個(gè)python文件了。。嗯
。操作hello.pyd根其他python文件是一樣的。
這樣就成功了。
如果碰到如下錯(cuò)誤,是因?yàn)橄到y(tǒng)找不到boost_python的dll。強(qiáng)烈建議把他們都扔到system32下!。
>>> import hello
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import hello
ImportError: DLL load failed: 找不到指定的模塊。
>>>
說(shuō)明,hello.cpp在boost\boost_1_34_0\libs\python\example\tutorial目錄下。里面的內(nèi)容是:
// 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);
}
是對(duì)greet從c++向python的一個(gè)封裝聲明吧,裝換就交給boost了。
先寫到這里了。下次再寫。。嗯