Boost. python compilation and example

Source: Internet
Author: User

Boost. python compilation and example

 

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 
 
  #include 
  
   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
 
  
# Include student. cppusing namespace boost: python; BOOST_PYTHON_MODULE (example) // python module {class _
  
   
(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.
 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.