Python's C/C + + extension -- boost UU Python writing Python module

In the previous section, python uses ctypes to directly call dynamic libraries and uses Python's C language API to encapsulate C functions. This article outlines the boost_python library that is convenient to encapsulate C + + classes for Python.

To install the boost python Library:

sudo aptitude install libboost-python-dev


The following code simply implements a normal function maxab() and a Student class:

#include <iostream>
#include <string>

int maxab(int a, int b) { return a>b?a:b; }

class Student {
        int age;
        std::string name;

        Student() {}
        Student(std::string const& _name, int _age) { name=_name; age=_age; }

        static void myrole() { std::cout << "I'm a student!" << std::endl; }

        void whoami() { std::cout << "I am " << name << std::endl; }

        bool operator==(Student const& s) const { return age == s.age; }
        bool operator!=(Student const& s) const { return age != s.age; }


Using the boost.python library to encapsulate is also very simple, as shown in the following code:

#include <Python.h>
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <vector>

#include "student.h"

using namespace boost::python;

BOOST_PYTHON_MODULE(student) {                                                                                                                                                                              
    // This will enable user-defined docstrings and python signatures,
    // while disabling the C++ signatures
    scope().attr("__version__") = "1.0.0";
    scope().attr("__doc__") = "a demo module to use boost_python.";
    docstring_options local_docstring_options(true, false, false);
            "maxab", &maxab, "return max of two numbers.\n"

    class_<Student>("Student", "a class of student")
        .def(init<std::string, int>())
        // methods for Chinese word segmentation
                "whoami", &Student::whoami, "method's doc string..."
                "myrole", &Student::myrole, "method's doc string..."
    // Package STL
    class_<std::vector<Student> >("StudentVec")
        .def(vector_indexing_suite<std::vector<Student> >())

The above code still include s the Python.h file. If not, an error will be reported:

wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory


There are two ways to compile the above code. One is to use g + + directly under the command line:

g++ -I/usr/include/python2.7  -fPIC wrap_student.cpp -lboost_python -shared -o

First, specify the path of Python.h. if it is Python 3, modify it to the corresponding path. When compiling wrap student.cpp, specify the - fPIC parameter, and link (- lboost Chu Python) to generate a dynamic library (- shared). The generated dynamic library can be directly import ed by python

In [1]: import student

In [2]: student.maxab(2, 5)
Out[2]: 5

In [3]: s = student.Student('Tom', 12)

In [4]: s.whoami()
I am Tom

In [5]: s.myrole()
I'm a student!
In addition, the method has always been to write the script with python's setuptools:

#!/usr/bin/env python

from setuptools import setup, Extension

    Extension("student", ["wrap_student.cpp"],                                                                                                                                                              
    libraries = ["boost_python"])

Then execute the command to compile:

python build
sudo python install

Article copyright belongs to Homo erectus

Tags: Python sudo C

Posted on Wed, 04 Dec 2019 07:32:06 -0500 by besa