Welcome reprint, Reproduced please specify the original address: http://blog.csdn.net/majianfei1023/article/details/46781581
Linux build boost Link: http://blog.csdn.net/majianfei1023/article/details/46761029
Compile and install the boost yesterday, ready to use Boost.python to write a python call C + + code sample, the result stepped on a lot of pits.
First put the code:
1.student.cpp, an ordinary C + + class
#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, the code that encapsulates C + + as a Python module, uses the Boost.python
#include <boost/python.hpp> #include "student.cpp" using namespace boost::p Ython; Boost_python_module (example) //python module {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
4.example.py,python Calling *.so Demo sample code
Import Examplestu = Example.student () stu.setname ("MJF") stu.setage (+) print Stu.nameprint stu.age
thought it would be smooth sailing. The result is a variety of tangled questions:
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 ' have not been declared
./boost/python/instance_holder.hpp:41:error:expected '; ' Before ' (' token
./boost/python/instance_holder.hpp:45:error: ' Pyobject ' have 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
A variety of search data found to be the problem of Python.
Missing dependent library Python-devel, to install:
sudo yum install Python-devel
2. Overcome the above problem. New problems have also been discovered.
/usr/bin/ld:cannot Find-lboost_python
A check, sure enough to find no libboost_python.so, installed boost when I was indeed completely installed. Do not know how to do, did not pretend to anticipate.
Once again, Boost.python.
. /bootstrap.sh--prefix=/home/mjf/lib
sudo./b2--with-python Install
Accomplished, took nearly two hours to solve some problems. Ability to successfully invoke example.so with Python
Finally: Thanks to StackOverflow, the answers to many questions can be found on top.
Boost.python Compiling and demonstrating sample