Linking Python and C++ with Boost.python

Introduction to Boost

Boost is a project to create open-source libraries for C++. There are a lot of nice tools available through Boost, one of which is Boost.python. Boost.Python provides seamless interoperability between C++ and Python. I have found it to be an extremely useful tool for scientific programming. Parts of a program that require more flexibility than speed can be written in Python, and critical portions can be coded in C++.

Tutorials and Examples

Unfortunately, the Boost.Python documentation is not written for beginners, and there is not a lot of help on other web sites. The mailing list is great, but a good mailing list does not replace good introductory documentation.

Hello World

Here is the “hello world” example from the Boost.python tutorial. I found that attempting to configure the Boost.jam build system is far more confusing than using GNU make to build the examples. The following presentation should be much easier to understand. All files to build this example can be found in the Shocksolution_Examples repo on GitHub.

Save the following as hello_ext.C

char const* greet()
{
   return "hello, world";
}
 
#include <boost/python.hpp>
 
BOOST_PYTHON_MODULE(hello_ext)
{
    using namespace boost::python;
    def("greet", greet);
}

Save the following as Makefile:

# location of the Python header files
 
PYTHON_VERSION = 2.7
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_ext
 
$(TARGET).so: $(TARGET).o
	g++ -shared -Wl,--export-dynamic $(TARGET).o -L$(BOOST_LIB) -lboost_python-$(PYTHON_VERSION) -L/usr/lib/python$(PYTHON_VERSION)/config -lpython$(PYTHON_VERSION) -o $(TARGET).so
 
$(TARGET).o: $(TARGET).C
	g++ -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).C

Save the following as test_hello.py

import hello_ext
print hello_ext.greet()

That’s it.

Other Examples

30 thoughts on “Linking Python and C++ with Boost.python

  1. Pingback: Configuring Boost::Python and Hello Boost « Paranoid Android

  2. Yati Sagade

    You saved me from the horrible hellhole this BoostJam is. It almost feels like it was crafted not to be used by anyone but the core developers. Thanks 🙂

    Reply
  3. Ross

    Craig, I expect you will get lots of traffic with your solution. I’ve been trying to get your files to compile, but I’m having a few Makefile issues. After editing with Nano to confirm tabs rather than spaces and converting to unix lifefeeds, I’m getting an error: make: hello_ext.o: Command not found. Any idea why make is viewing $(TARGET).o as a command?

    Reply
  4. Craig Post author

    Ross, the formatting of the Makefile in this post was causing a lot of problems. I have refomatted the Makefile in the post and added all necessary files for the example to my repo on GitHub (link is near the top of the post).

    Also, the version of Boost on my workstation now has separate library files for each Python version, so I modified the Makefile accordingly. Let me know if there are any further problems.

    Reply
  5. Ross

    Craig, brilliant work. After a few mods to my own python and boost lib paths, and checking the whitespace/tabs formatting, your Makefile worked like a charm. With no disrespect to the bjam fellows, I worked on bjam until I was about to pass out and realized I could probably build using good-old-fashioned make (with a little help). That’s when a search took me to your site. Thanks for unblocking the road.

    Reply
  6. Bharat

    I am getting error /usr/local/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory
    # include

    Any idea what could be the problem here?

    Reply
    1. Craig Post author

      There is probably a problem with the configuration of your include paths. I suspect that the compiler cannot find the source directory with the Python header files, so you may need to set an environment variable or use a command-line flag to tell it where to find the header files.

      Reply
  7. Menaka

    hi! I get this error when i try to make it.

    g++ -shared -Wl,–export-dynamic hello_ext.o -L/usr/lib -lboost_python-2.7 -L/usr/lib/python2.7/config -lpython2.7 -o hello_ext.so
    /usr/bin/ld: cannot find -lboost_python-2.7
    collect2: ld returned 1 exit status
    make: *** [hello_ext.so] Error 1

    please help me! thanks in advance.

    Reply
    1. Craig Post author

      The linker cannot find the library called boost_python-2.7. You should find the path to this library on your system and provide that path to the linker with a -L flag as you have done with the other library paths. If that library doesn’t exist, it may need to be installed, or you may have specified its name incorrectly.

      Reply
    2. Nima Hazar

      in the Makefile try -lboost_python instead of -lboost_python-$(PYTHON_VERSION)
      In my case the boost_python library name does not have the python version as a suffix.

      Reply
    1. Craig Post author

      Make sure your Makefile is identical to the one I posted on GitHub. Note that spaces and tabs are critical! The –export-dynamic option is a linker (LD) flag, NOT a compiler (GCC) flag. You pass flags to the linker by giving GCC the flag “-Wl,” followed by the desired linker flag. There cannot be a space between the comma and the linker flag.

      Reply
  8. TC

    This is great, thank you for taking the time to make a walk through. The Boost documentation is cryptic for those attempting to use the libraries for the first time.

    I’ve made it past a few of the common errors that I’m seeing on this thread and elsewhere, but I’m getting this error:

    g++ -I/Users/tomconlin/anaconda/pkgs/python-2.7.11-0/include/python2.7 -I/usr/include -fPIC -c hello_ext.C
    g++ -shared -Wl,–export-dynamic hello_ext.o -L/usr/lib -lboost_python -L/usr/lib/python2.7/config -lpython2.7 -o hello_ext.so
    ld: unknown option: –export-dynamic
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    make: *** [hello_ext.so] Error 1

    Has anyone seen anything like this before? Any insights would be greatly appreciated!!

    Thanks again for the tutorial, simple and to the point!

    Reply
  9. HARIHARAN RAGOTHAMAN

    I am getting the following. It would be great if you could tell me what happens here.

    ImportError: /scratch/Python_stuff/Python_Boost/hello_ext.so: undefined symbol: _ZN5boost6python6detail11init_mod uleEPKcPFvvE

    Reply
  10. Alam

    Hi

    I downloaded all the files and tried to run the python file. But it is giving me following error:

    ImportError: No module named hello_ext

    I am not sure whether Boost Python is installed on my system. I tried to install it using BoostPython website but it did not work. Can someone tell me about the installtion of boost python library for python 2.7? It would be great if you could also help me in troublehooting this error.

    Thanks in anticipation.

    Reply
  11. ArunV

    I had the same issue with “ImportError” (working on the new windows subsystem for Linux for Windows 10). It was related to not linking the correct library name (boost_python-py27 not boost_python-2.7 per the Makefile example above) and the fact that my boost-python link path was set incorrectly (ie not -L/usr/lib but -L/usr/lib/x86_64-linux-gnu). Hope this helps Hari and Alam…

    Reply
  12. Prakash

    Hi,

    I tried running your files in git. But its giving me an error.

    ImportError: No module named hello_ext. I have boost installed on my system and I am trying to run this program on Windows 7. Any help will be appreciated

    Reply
    1. Craig Post author

      Problems like this are very specific to the way that libraries are installed and configured on your particular system, so I can’t help with this type of problem. Also, I have never done this on a Windows system.

      Reply

Leave a Reply