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’ 错误

 

posted on 2012-05-15 13:31  bian  阅读(479)  评论(0编辑  收藏  举报

导航