Boost.Python

 

坏境python25 + vs2005 (2005真耗资源阿。。。)

有一段时间没写blog了。这几天都在研究怎么封装c++,让python可以用c++的库。在网上发现了boost.python这个好咚咚。不

过在使用过程中碰到一点问题。本文教大家如何把

1.           char const* greet() 
2.           { 
3.              return "hello, world"; 
4.           } 

封装成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目录下。里面的内容是:

1.           //  Copyright Joel de Guzman 2002-2004. Distributed under the Boost 
2.           //  Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt 
3.           //  or copy at http://www.boost.org/LICENSE_1_0.txt) 
4.           //  Hello World Example from the tutorial 
5.           //  [Joel de Guzman 10/9/2002] 
6.            
7.           char const* greet() 
8.           { 
9.              return "hello, world"; 
10.       } 
11.        
12.       #include <boost/python/module.hpp> 
13.       #include <boost/python/def.hpp> 
14.       using namespace boost::python; 
15.        
16.       BOOST_PYTHON_MODULE(hello) 
17.       { 
18.           def("greet", greet); 
19.       } 

其中

1.           BOOST_PYTHON_MODULE(hello) 
2.           { 
3.               def("greet", greet); 
4.           } 

是对greet从c++向python的一个封装声明吧,装换就交给boost了。

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值