用boost.python为python写c\c++扩展曲折配置最终成功历程

捣鼓了1天多才完成,sign。。。。
正确步骤如下:

1 安装boost.python
单独编译boost.python:
bjam -sTOOLS=gcc --with-python --build-type=complete
编译所有:
bjam -sTOOLS=gcc --build-type=complete
清除所有编译:
bjam -sTOOLS=gcc --clean
清除boost.python的编译文件:
bjam -sTOOLS=gcc --with-python --with-python

默认情况下,会在boost源文件下创建了bin.v2文件夹,该文件夹为编译后的二进制库
lwj@lwj-desktop:~/boost_1_37_0/bin.v2

2 创建工程
cd 到 boost_1_37_0/libs/python/example/quickstart
里面是一个样板项目,最为重要的2个文件是Jamroot 和 boost-build.jam
每个项目都要有这两个文件,里面需要配置boost目录。
我的boost-build.jam的内容:
  1. boost-build/home/lwj/boost_1_37_0/tools/build/v2;
Jamroot的内容:
  1. #CopyrightDavidAbrahams2006.DistributedundertheBoost
  2. #SoftwareLicense,Version1.0.(Seeaccompanying
  3. #fileLICENSE_1_0.txtorcopyathttp://www.boost.org/LICENSE_1_0.txt)
  4. #SpecifythepathtotheBoostproject.Ifyoumovethisproject,
  5. #adjustthepathtorefertotheBoostrootdirectory.
  6. use-projectboost
  7. :/home/lwj/boost_1_37_0;
  8. #Setuptheproject-widerequirementsthateverythingusesthe
  9. #boost_pythonlibrarydefinedintheprojectwhoseglobalIDis
  10. #/boost/python.
  11. projecthello_ext
  12. :requirements<library>/boost/python//boost_python
  13. ;
  14. #Makethedefinitionofthepython-extensionruleavailable
  15. importpython;
  16. #DeclareaPythonextensioncalledhello.
  17. python-extensionhello_ext:hello.cpp;
  18. #DeclareanexecutablecalledembeddingthatembedsPython
  19. #exeembedding:embedding.cpp/python//python;
  20. #importtesting;
  21. #Declareatestoftheextensionmodule
  22. #testing.make-testrun-pyd:extendingtest_extending.py::test_ext;
  23. #Declareatestoftheembeddingapplication
  24. #testing.runembedding
  25. #:#anyordinaryarguments
  26. #:script.py#anyargumentsthatshouldbetreatedasrelativepaths
  27. #:#requirements
  28. #:test_embed;#nameoftest
  29. #Createa"test"targetthatrunsallthetests
  30. #aliastest:test_exttest_embed;
  31. #makesurethetestsdon'trunbydefault
  32. #explicittest_exttest_embedtest;
  33. #Alittle"rule"(function)tocleanupthesyntaxofdeclaringtests
  34. #oftheseextensionmodules.
  35. localrulerun-test(test-name:sources+)
  36. {
  37. importtesting;
  38. testing.make-testrun-pyd:$(sources)::$(test-name);
  39. }
  40. #Declaretesttargets
  41. run-testhello:hello_exthello.py;


3 使用bjam编译工程
创建 hello.cpp 文件
  1. /*
  2. *=====================================================================================
  3. *
  4. *Filename:hello.cpp
  5. *
  6. *Description:testboost.python
  7. *
  8. *Version:1.0
  9. *Created:2008年12月17日14时23分41秒
  10. *Revision:none
  11. *Compiler:gcc
  12. *
  13. *Author:LiWeiJian(mn),lwj1396@163.com
  14. *Company:hunanuniversity
  15. *
  16. *=====================================================================================
  17. */
  18. #include<boost/python.hpp>
  19. charconst*greet()
  20. {
  21. return"helloworld";
  22. }
  23. BOOST_PYTHON_MODULE(hello_ext)
  24. {
  25. usingnamespaceboost::python;
  26. def("greet",greet);
  27. }
创建hello.py
  1. importhello_ext
  2. foriinrange(10):
  3. printhello_ext.greet()
模块hello_ext为c++所wrap.


在工程目录下,输入bjam
或者 bjam --preserve-test-targets 这样不会删除可执行文件(我这里是so文件)

编译若成功,会产生bin目录,该目录有gcc-4.2.4 hello.test 文件夹,

hello.test 为测试代码,里面有测试结果,测试结果为文本文件。
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world

EXIT STATUS: 0


gcc-4.2.4文件夹中:
包含了hello_ext.so 文件,该文件可以做为python模块导入

4 运行加载库
进入hello_ext.so的目录,调出python命令行,结果会出错,信息如下:

lwj@lwj-desktop:~/code/python/embedc/helloworld/bin/gcc-4.2.4/debug$ ls
hello_ext.so hello.o
lwj@lwj-desktop:~/code/python/embedc/helloworld/bin/gcc-4.2.4/debug$ python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello_ext
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: libboost_python-gcc42-d-1_37.so.1.37.0: cannot open shared object file: No such file or directory
>>>


原因是系统不认得boost.python的安装路径,因此需要设置 LD_LIBRARY_PATH
可以在控制台中输入 export LD_LIBRARY_PATH=path_to_your_libboost_python-gcc41-1_37.so.1.37.0
则此次会话可以成功import hello_ext
并且调用hello_ext.greet()方法

为使得每次终端都认得这个boost.python库的路径
linux也可以支持“加载当前目录的动态库”。只要设置合适的环境变量LD_LIBRARY_PATH就可以了。设置方法有以下三种:
1、临时修改,log out之后就失效 B3yLinux联盟
在terminal中执行:export LD_LIBRARY_PATH=./
2、让当前帐号以后都优先加载当前目录的动态库 B3yLinux联盟
修改~/.bash_profile在文件末尾加上两行: LD_LIBRARY_PATH=./ 和 export LD_LIBRARY_PATH
3、让所有帐号从此都优先加载当前目录的动态库 B3yLinux联盟
修改/etc/profile在文件末尾加上两行: LD_LIBRARY_PATH=./ 和 export LD_LIBRARY_PATH
PS:修改ld.so.conf不能达到我们的目的,因为ld.so.conf只支持绝对路径。




最终。。。。混编之旅变开始拉,哈哈哈哈哈哈哈哈

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值