boost.python编译及演示样例
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了boost.python编译及演示样例,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2736字,纯文字阅读大概需要4分钟。
内容图文
欢迎转载,转载请注明原文地址:http://blog.csdn.net/majianfei1023/article/details/46781581
linux编译boost的链接:http://blog.csdn.net/majianfei1023/article/details/46761029
昨天编译安装好boost,今天准备使用boost.python写个python调用c++代码的样例,结果踩了非常多坑。
首先贴上代码:
1.student.cpp,一个普通的c++类
#include <iostream> #include <string> using namespace std; class student { public: void setname(string str) { name_ = str; } string getname() { return name_; } void setage(int age) { age_ = age; } int getage() { return age_; } private: string name_; int age_; };
2.student2py.cpp,把c++封装成python模块的代码,使用了boost.python
#include <boost/python.hpp> #include "student.cpp" using namespace boost::python; BOOST_PYTHON_MODULE(example) //python模块 { class_<student>("student") .def("setname",&student::setname) .def("getname",&student::getname) .def("setage",&student::setage) .def("getage",&student::getage) .add_property("name",&student::getname,&student::setname) .add_property("age",&student::getage,&student::setage) ; }
example.so:student.o student2py.o g++ student2py.o -o example.so -shared -fPIC -I/usr/include/python2.6 -I/home/mjf/lib/include -L/usr/lib/python2.6 -L/home/mjf/lib/lib -lboost_python student.o: g++ -c student.cpp -o student.o student2py.o:student.o g++ -c student2py.cpp -o student2py.o -fPIC -I/usr/include/python2.6 -I/home/mjf/lib/include clean: rm -rf student.o student2py.o rm -rf example.so
import example stu = example.student() stu.setname("mjf") stu.setage(25) print stu.name print stu.age
1.
../boost/python/detail/wrap_python.hpp:50:23: error: pyconfig.h: No such file or directory
./boost/python/detail/wrap_python.hpp:75:24: error: patchlevel.h: No such file or directory
./boost/python/detail/wrap_python.hpp:78:2: error: #error Python 2.2 or higher is required for
./boost/python/detail/wrap_python.hpp:142:21: error: Python.h: No such file or directory
./boost/python/instance_holder.hpp:34: error: ‘PyObject’ has not been declared
./boost/python/instance_holder.hpp:41: error: expected ‘;’ before ‘(’ token
./boost/python/instance_holder.hpp:45: error: ‘PyObject’ has not been declared
./boost/python/detail/wrapper_base.hpp:21: error: expected initializer before ‘*’ token
./boost/python/detail/wrapper_base.hpp:23: error: expected initializer before ‘*’ token
各种查资料发现是python的问题。
缺少依赖库 python-devel,要安装一下:
sudo yum install python-devel
2.攻克了上面的问题。又发现了新的问题。
/usr/bin/ld: cannot find -lboost_python
一查,果然发现没有libboost_python.so,安装boost的时候我的确是全然安装的。不知道怎么搞的,没装好预计。
又一次装了一下boost.python
./bootstrap.sh --prefix=/home/mjf/lib
sudo ./b2 --with-python install
大功告成,花了接近两个小时解决一些问题。能够成功用python调用example.so
最后:感谢stackoverflow,非常多问题的答案都能在上面找得到。
内容总结
以上是互联网集市为您收集整理的boost.python编译及演示样例全部内容,希望文章能够帮你解决boost.python编译及演示样例所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。