boost.python 使用

本文详细介绍了如何在Ubuntu环境下安装并配置Python开发环境、Boost.Python库,以及创建和使用Boost.Python模块的具体步骤。通过示例代码展示了如何定义一个简单的Python函数并在Python环境中调用它。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值