首页 > 学院 > 开发设计 > 正文

ubuntuboost.python

2019-11-14 17:28:20
字体:
来源:转载
供稿:网友

安装boost(未尝试只安装 libboost-python-dev)

sudo apt-get install libboost-all-dev

新建hello_ext.cpp,输入以下代码

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

存储,使用以下命令行编译:

g++ -I/usr/include/python2.7 -c -fPIC hello_ext.cpp -o hello_ext.og++ -shared -o hello_ext.so hello_ext.o -lpython2.7 -lboost_python

 

在hello_ext所在目录,打开 python shell

>>> import hello_ext

>>> PRint hello_ext.greet()

 

注意事项:

- 要添加 -lpython2.7 和 -lboost_python,否则会出现一个很复杂的函数找不到的问题,参见 http://stackoverflow.com/questions/1771063/no-such-file-or-directory-error-with-boost-python

很好的示例代码: https://github.com/TNG/boost-python-examples


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表