Boost. python compilation and example, boost. python example
Welcome to reprint, reprint please indicate the original address: http://blog.csdn.net/majianfei1023/article/details/46781581
Linux compiled boost link: http://blog.csdn.net/majianfei1023/article/details/46761029
Yesterday, we compiled and installed boost. Today we are going to use boost. python to write an example of python Calling c ++ code. The result is a lot of pitfall.
First, paste the Code:
1. student. cpp, a common 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, which encapsulates c ++ as the code of the python module and uses boost. python
# Include <boost/python. hpp> # include "student. cpp "using namespace boost: python; 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
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 calls the sample code of *. so.
import examplestu = example.student()stu.setname("mjf")stu.setage(25)print stu.nameprint stu.age
I thought it was smooth sailing. As a result, there were various tangled problems in 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
./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
Various types of data query and discovery are python problems.
If the dependent library python-devel is missing, install it:
Sudo yum install python-devel
2. Solved the above problems and found new problems,
/Usr/bin/ld: cannot find-lboost_python
I found that there was no libboost_python.so. I did install it completely when installing boost. I don't know how to do it. I didn't have an estimate. Reinstall boost. python.
./Bootstrap. sh -- prefix =/home/mjf/lib
Sudo./b2 -- with-python install
It took nearly two hours to solve some problems. You can use python to call example. so.
Last: ThanksStackoverflowYou can find answers to many questions.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.