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

21 篇文章 0 订阅
18 篇文章 0 订阅
捣鼓了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. # Copyright David Abrahams 2006. Distributed under the Boost
  2. # Software License, Version 1.0. (See accompanying
  3. # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. # Specify the path to the Boost project.  If you move this project,
  5. # adjust the path to refer to the Boost root directory.
  6. use-project boost 
  7.   : /home/lwj/boost_1_37_0 ;
  8. # Set up the project-wide requirements that everything uses the
  9. # boost_python library defined in the project whose global ID is
  10. # /boost/python.
  11. project hello_ext 
  12.   : requirements <library>/boost/python//boost_python 
  13.   ;
  14. # Make the definition of the python-extension rule available
  15. import python ;
  16. # Declare a Python extension called hello.
  17. python-extension hello_ext : hello.cpp ;
  18. # Declare an executable called embedding that embeds Python
  19. #exe embedding : embedding.cpp /python//python ;
  20. #import testing ;
  21. # Declare a test of the extension module
  22. #testing.make-test run-pyd : extending test_extending.py : : test_ext ;
  23. # Declare a test of the embedding application
  24. #testing.run embedding 
  25. #  :              # any ordinary arguments
  26. #  : script.py    # any arguments that should be treated as relative paths
  27. #  :              # requirements
  28. #  : test_embed ; # name of test
  29. # Create a "test" target that runs all the tests
  30. #alias test : test_ext test_embed ;
  31. # make sure the tests don't run by default
  32. #explicit test_ext test_embed test ;
  33. # A little "rule" (function) to clean up the syntax of declaring tests
  34. # of these extension modules.
  35. local rule run-test ( test-name : sources + )
  36. {
  37.     import testing ;
  38.     testing.make-test run-pyd : $(sources) : : $(test-name) ;
  39. }
  40. # Declare test targets
  41. run-test hello : hello_ext hello.py ;
 

3 使用bjam编译工程
   创建 hello.cpp 文件
  
  1. /*
  2.  * =====================================================================================
  3.  *
  4.  *       Filename:  hello.cpp
  5.  *
  6.  *    Description:  test boost.python
  7.  *
  8.  *        Version:  1.0
  9.  *        Created:  2008年12月17日 14时23分41秒
  10.  *       Revision:  none
  11.  *       Compiler:  gcc
  12.  *
  13.  *         Author:  Li WeiJian (mn), lwj1396@163.com
  14.  *        Company:  hunan university
  15.  *
  16.  * =====================================================================================
  17.  */
  18. #include<boost/python.hpp>
  19. char const* greet()
  20. {
  21.     return "hello world";
  22. }
  23. BOOST_PYTHON_MODULE(hello_ext)
  24. {
  25.     using namespace boost::python;
  26.     def("greet",greet);
  27. }
   创建hello.py
  1. import hello_ext
  2. for i in range(10):
  3.     print hello_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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值