Python Language C Extension Using c++ and Boost


This is a basic example of a C Extension using C++ and Boost.

C++ Code

C++ code put in hello.cpp:

#include <boost/python/module.hpp>
#include <boost/python/list.hpp>
#include <boost/python/class.hpp>
#include <boost/python/def.hpp>

// Return a hello world string.
std::string get_hello_function()
   return "Hello world!";

// hello class that can return a list of count hello world strings.
class hello_class

   // Taking the greeting message in the constructor.
   hello_class(std::string message) : _message(message) {}

   // Returns the message count times in a python list.
   boost::python::list as_list(int count)
      boost::python::list res;
      for (int i = 0; i < count; ++i) {
      return res;
   std::string _message;

// Defining a python module naming it to "hello".
   // Here you declare what functions and classes that should be exposed on the module.

   // The get_hello_function exposed to python as a function.
   boost::python::def("get_hello", get_hello_function);

   // The hello_class exposed to python as a class.
   boost::python::class_<hello_class>("Hello", boost::python::init<std::string>())
      .def("as_list", &hello_class::as_list)

To compile this into a python module you will need the python headers and the boost libraries. This example was made on Ubuntu 12.04 using python 3.4 and gcc. Boost is supported on many platforms. In case of Ubuntu the needed packages was installed using:

sudo apt-get install gcc libboost-dev libpython3.4-dev

Compiling the source file into a .so-file that can later be imported as a module provided it is on the python path:

gcc -shared -o -fPIC -I/usr/include/python3.4 hello.cpp -lboost_python-py34 -lboost_system

The python code in the file

import hello


h = hello.Hello("World hello!")

Then python3 will give the following output:

Hello world!
['World hello!', 'World hello!', 'World hello!']