您的位置:首页 > 编程语言 > C语言/C++

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

2015-04-01 15:55 369 查看
坏境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了。

先写到这里了。下次再写。。嗯
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: