Boost.python Compiling and demonstrating sample

Source: Internet
Author: User

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

Related Article

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.