使用boost.bython扩充python

上一篇 / 下一篇  2015-08-31 22:13:07 / 个人分类:测试自动化

1、第一步,先要准备boost库,编译库,需要编译python库,我使用python的环境是python2.7。boost是1.5.7版本。存放在目录为:/Volumes/data/code/c++/boost_1_57_0。boost的编译,网上有一大把。这里就不详细说了。

说明:如果你的mac机器 /usr/include头文件很少,你可以使用下面这个命令安装。


xcode-select--install


2、编写一个hello_world.cpp,代码如下:

  1. ​#include<iostream>
  2. #include<string>
  3. #include<boost/python.hpp>

  4. usingnamespacestd;
  5. usingnamespaceboost::python;

  6. voidsay()
  7. {
  8.    cout<<"Hello World!"<<endl;
  9. }

  10. BOOST_PYTHON_MODULE(hello_world)
  11. {
  12.    def("say",say);
  13. }


3、编译,这个时候生成hello_world.so文件。

  1. g++-fpic-c-L/Volumes/data/code/c++/boost_1_57_0/-I/usr/include/python2.7hello_world.cpp
  2. g++-shared-L/usr/lib-L/Volumes/data/code/c++/boost_1_57_0/stage/lib-lpython2.7-lboost_python-o hello_world.so hello_world.o

4、把/Volumes/data/code/c++/boost_1_57_0/stage/lib/libboostpython.dylib拷贝到hello_world.so同一个目录下。测试新开发的模块。如下:

  1. fish:c++fish$ python
  2. Python2.7.10(default,Jul142015,19:46:27)
  3. [GCC4.2.1CompatibleAppleLLVM6.0(clang-600.0.39)]on darwin
  4. Type"help","copyright","credits"or"license"formore information.
  5. >>>importhello_world
  6. >>>hello_world.say()
  7. HelloWorld!
  8. >>>
  9. >>>quit()

结束语:mac/linux使用boost.python库扩展python看上去比较简单,但由于对gcc编译命令不熟悉,很多不知道,因此摸索了很久。写这里给有需要的同学使用。

如果你熟悉c/c++,不给python进行扩展,即使不太熟悉python,也可以慢慢熟悉python了。后续,再总结,如何在c/c++中使用python,嵌入式python


TAG: 扩展python 嵌入式python

 

评分:0

我来说两句

日历

« 2024-04-30  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 41828
  • 日志数: 10
  • 建立时间: 2015-08-24
  • 更新时间: 2016-06-21

RSS订阅

Open Toolbar