织梦CMS - 轻松建站从此开始!

罗索

很好的c++和Python混合编程文章(4)

jackyhwei 发布于 2010-06-03 21:05 点击:次 
c++中嵌入python入门4 之 Boost.Python 坏境python25 + vs2005 (2005真耗资源阿。。。) 有一段时间没写blog了。这几天都在研究怎么封装c++,让python可以用c++的库。在网
TAG:

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

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

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

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

  1. char const* greet() 
  2.    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目录下。里面的内容是:

  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.    return "hello, world"
  9.  
  10. #include <boost/python/module.hpp> 
  11. #include <boost/python/def.hpp> 
  12. using namespace boost::python; 
  13.  
  14. BOOST_PYTHON_MODULE(hello) 
  15.     def("greet", greet); 

其中

  1. BOOST_PYTHON_MODULE(hello) 
  2.     def("greet", greet); 

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

先写到这里了。下次再写。。嗯 (jacky2019)

本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201006/9574.html]
本文出处:cppblog.com 作者:jacky2019
顶一下
(4)
80%
踩一下
(1)
20%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容