boost.python編譯及示範範例

來源:互聯網
上載者:User

標籤:

歡迎轉載,轉載請註明原文地址: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);}


3.makefile

example.so:student.o student2py.og++ 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_pythonstudent.o:g++ -c student.cpp -o student.o student2py.o:student.og++ -c student2py.cpp -o student2py.o -fPIC -I/usr/include/python2.6 -I/home/mjf/lib/includeclean:rm -rf student.o student2py.orm -rf example.so 


4.example.py,python調用*.so的示範範例代碼

import examplestu = example.student()stu.setname("mjf")stu.setage(25)print stu.nameprint stu.age


本來以為一帆風順。結果make的時候出了各種糾結的問題:

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編譯及示範範例

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.