boost.python 使用

来源:互联网 发布:视频文件加密软件 编辑:程序博客网 时间:2024/06/10 12:02

(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!!!'
>>>




原创粉丝点击