Kisser Leon

这个kisser不太冷
posts - 100, comments - 102, trackbacks - 0, articles - 0

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

Posted on 2007-06-01 10:32 kk 阅读(6647) 评论(5)  编辑 收藏 引用 所属分类: C++

坏境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了。

 

先写到这里了。下次再写。。嗯

Feedback

# re: c++中嵌入python入门4 之 Boost.Python  回复  更多评论   

2007-06-01 10:33 by 小熊
这个标题好像有点问题哈, 应该不是c++中嵌入python了哦,呵呵

# re: c++中嵌入python入门4 之 Boost.Python  回复  更多评论   

2007-06-05 19:22 by ken
为什么我嵌入python到c++时,总是找不到python.h文件。能不详细的环境需求设置说一下吗?可以联系我指导下吗?QQ104357894
email:kxting@163.com

# re: c++中嵌入python入门4 之 Boost.Python  回复  更多评论   

2007-06-06 15:04 by Allen
你应该设好编译环境,作者在1中有介绍,看我用MinGW编译过程:
gcc -Id:/python25/include -Id:/MinGW/include -c test2.c
gcc -o test2.exe test2.o -Ld:/python25/libs -lpython25

# re: c++中嵌入python入门4 之 Boost.Python[未登录]  回复  更多评论   

2007-08-28 01:31 by Ken
请教一个问题,我现在有一个Windows应用程序,我想通过Python来扩展功能,那么Python需要访问到这个应用程序里的数据,也就是通过Python访问一个EXE里的函数,LUA是可以这样做的,不知道Python是否可以,谢谢

# re: c++中嵌入python入门4 之 Boost.Python  回复  更多评论   

2008-04-29 17:25 by paul law
using python ;
这个里面的空格也要打才行!!!!晕。少了一个空格。用了一个下午的时间

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理