Need to implement some code in Cpp for efficiency, but still want to use Python to rapidly develop the software. Test the boost.python lib to convert Cpp functions to python module. Take reference example from boost.python simple example and the makefile from Craig.

The hello_py.cpp File defines the module for each cpp functions

#include <boost/python.hpp>
#include "hello.hpp"

using namespace boost::python;

    //add regular functions to the module
    def("greet", greet);
    def("square", square);

hello.hpp defines the cpp functions, included in the above file

#include <string>

    //A couple of simple C++ functions that we want to expose to Python
    std::string greet(){
        return "hello, world";

    int square(int number){
        return number*number;

The makefile used to build a shared library. The boost python lib might have different path or file name, need to change accordingly.

#location of the Python header files
PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION)

#location of the Boost Python include files and library
BOOST_INC = /usr/include
BOOST_LIB = /usr/lib

#compile mesh classes
TARGET = hello_py

$(TARGET).so: $(TARGET).o
    g++ -shared -Wl, --export-dynamic $(TARGET).o -L$(BOOST_LIB) -lboost_python-mt-py27 -L/usr/lib/python$(PYTHON_VERSION)/config -lpython$(PYTHON_VERSION) -o $(TARGET).so

    g++ -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp

To test the module, export the shared library to PYTHONPATH first. Then try:

>>>import hello_py
>>>hello_py.greet() 'hello world'
>>>hello_py.square(2) 4