linux下使用boost python (直接用g++生成动态库,不使用bjam)

仿照 http://www.boost.org/doc/libs/1_49_0/libs/python/doc/tutorial/doc/html/index.html 写一个例子

 1 #include <boost/python.hpp>
 2 
 3 char const* greet()
 4 {
 5    return "hello, world";
 6 }
 7 BOOST_PYTHON_MODULE(hello_ext)
 8 {
 9     using namespace boost::python;
10     def("greet", greet);
11 }

 

保存为hello_ext.cpp

 

然后用g++生成动态库

 g++ hello_ext.cpp -shared -fPIC -o hello_ext.so -I /usr/include/python2.6 -lboost_python

 

当前目录下会生成一个hello_ext.so

 

然后就可以在python里使用了

>>>import hello_ext

>>>hello_ext.greet()

‘hello, world’

>>>

 

注意,BOOST_PYTHON_MODULE里的导出模块(hello_ext)必须与生成的so的名字相同,否则在python里import的时候会报‘dynamic module does not define init function’ 错误

 

转载于:https://www.cnblogs.com/bianjunjie/archive/2012/05/15/2501436.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值