设为首页 收藏本站
查看: 1443|回复: 0

[经验分享] 用boost.python为python写c\c++扩展曲折配置最终成功历程

[复制链接]

尚未签到

发表于 2017-4-29 10:23:08 | 显示全部楼层 |阅读模式
捣鼓了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的内容:

  • boost-build/home/lwj/boost_1_37_0/tools/build/v2;

Jamroot的内容:

  • #CopyrightDavidAbrahams2006.DistributedundertheBoost
  • #SoftwareLicense,Version1.0.(Seeaccompanying
  • #fileLICENSE_1_0.txtorcopyathttp://www.boost.org/LICENSE_1_0.txt)

  • #SpecifythepathtotheBoostproject.Ifyoumovethisproject,
  • #adjustthepathtorefertotheBoostrootdirectory.
  • use-projectboost
  • :/home/lwj/boost_1_37_0;

  • #Setuptheproject-widerequirementsthateverythingusesthe
  • #boost_pythonlibrarydefinedintheprojectwhoseglobalIDis
  • #/boost/python.
  • projecthello_ext
  • :requirements<library>/boost/python//boost_python
  • ;

  • #Makethedefinitionofthepython-extensionruleavailable
  • importpython;

  • #DeclareaPythonextensioncalledhello.
  • python-extensionhello_ext:hello.cpp;

  • #DeclareanexecutablecalledembeddingthatembedsPython
  • #exeembedding:embedding.cpp/python//python;

  • #importtesting;

  • #Declareatestoftheextensionmodule
  • #testing.make-testrun-pyd:extendingtest_extending.py::test_ext;

  • #Declareatestoftheembeddingapplication
  • #testing.runembedding
  • #:#anyordinaryarguments
  • #:script.py#anyargumentsthatshouldbetreatedasrelativepaths
  • #:#requirements
  • #:test_embed;#nameoftest

  • #Createa"test"targetthatrunsallthetests
  • #aliastest:test_exttest_embed;

  • #makesurethetestsdon'trunbydefault
  • #explicittest_exttest_embedtest;

  • #Alittle"rule"(function)tocleanupthesyntaxofdeclaringtests
  • #oftheseextensionmodules.
  • localrulerun-test(test-name:sources+)
  • {
  • importtesting;
  • testing.make-testrun-pyd:$(sources)::$(test-name);
  • }

  • #Declaretesttargets
  • run-testhello:hello_exthello.py;





3 使用bjam编译工程
创建 hello.cpp 文件

  • /*
  • *=====================================================================================
  • *
  • *Filename:hello.cpp
  • *
  • *Description:testboost.python
  • *
  • *Version:1.0
  • *Created:2008年12月17日14时23分41秒
  • *Revision:none
  • *Compiler:gcc
  • *
  • *Author:LiWeiJian(mn),lwj1396@163.com
  • *Company:hunanuniversity
  • *
  • *=====================================================================================
  • */

  • #include<boost/python.hpp>
  • charconst*greet()
  • {
  • return"helloworld";
  • }

  • BOOST_PYTHON_MODULE(hello_ext)
  • {
  • usingnamespaceboost::python;
  • def("greet",greet);
  • }

创建hello.py

  • importhello_ext

  • foriinrange(10):
  • 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只支持绝对路径。




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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-370677-1-1.html 上篇帖子: Python 终端下的TUI开发,whiptail 的 Python 封装 下篇帖子: 服务器暂时无法响应您的请求 500 Internal Server Error
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表