您的位置:首页 > 编程语言 > Python开发

boost.python 使用

2013-07-26 16:55 176 查看
(1) python-dev 安装

sudo apt-get install python-dev

(2) boost.python 库的安装

sudo ./bjam toolset=gcc  --build-type=complete --layout=versioned  --with-python install

(3)  boost_python_test.cpp

#include <boost/python.hpp>

#include <Python.h>

#include <string>

std::string hello_func()

{

    return "GET BOOST!!!";

}

using namespace boost::python;

// export interface

BOOST_PYTHON_MODULE(emo)

{

   // like python function defition

   def("hello", hello_func, "greet");

}

编译成动态库

g++ -c -fPIC boost_python_test.cpp -I/usr/local/include/boost-1_53/ -I/usr/include/python2.7 -o boost_python_test.o

g++ -shared -Wl,-soname,emo.so -o emo.so  boost_python_test.o -lpython2.7 -lboost_python-gcc46-1_53

注意: 如果没有-lpython2.7

Traceback (most recent call last):

  File "export.py", line 3, in <module>

    import emo

ImportError: /home/xuebingyuan/project/python/emo.so: undefined symbol: _ZNK5boost6python7objects21py_function_impl_base9max_arityEv
http://stackoverflow.com/questions/1780003/import-error-on-boost-python-hello-program
(4)  import emo后

Traceback (most recent call last):

  File "export.py", line 3, in <module>

    import emo

ImportError: libboost_python-gcc46-1_53.so.1.53.0: cannot open shared object file: No such file or directory

cd /etc/ld.so.conf.d

vim boost.conf #这里写上boost库编译生成的库路径/usr/local/lib

ldconfig
http://bbs.python123.com/thread-589-1-1.html
(5) python

>>> import emo

>>> help(emo)

>>> emo.hello()

'GET BOOST!!!'

>>>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: