[SciPy-user] FYI: C++ Extensions for Python

David Grant david.grant at telus.net
Mon Nov 1 17:51:05 EST 2004


Just thought I'd share my experiences concerning looking for a way to 
make python interfaces for C++ code (see my previous thread about this):

pycxx - Looks like it requires one to modify all the C++ source, adding 
PyObject pointers everywhere.  I stayed away from this.  I don't want to 
modify the code if I have to (why should I have to?  the reason I'm 
making an interface is so that I can use the C++ code, and not touch it, 
thus avoiding creating bugs in the C++ code.  If I WANTED to touch the 
C++ code I'd just re-write in python).

boost.python - Horrible documentation and horrible setup.  Nothing good 
to say about this.  I spent more time on this than any other method and 
made the least progress.

weave - was going to write a python routine which had some small piece 
weave code.  The weave code would be talking to a C++ library or 
compiled python extension or something.  I was able to make a C++ 
library containing all my C++ code but then abandoned it after that, to 
see if there were simpler methods

python.org method - http://docs.python.org/ext/simpleExample.html - 
Similar to pycxx it involved altering the code

swig - at first it seemed like a lot of work to copy everything from the 
.h files into the .i file.  But then I realized you can just use 
%include and it will use that stuff for swig.  Simple examples are given 
in the docs for both C and C++ which work well.  I like how it just 
generates an extra .c wrapper file which is then compiled to a .o file 
and linked together will all the other .o files and libraries (like 
-llapack) into a python extension, very simply.  Extend any methods or 
functions you want.  I had a problem because the C++ class overloaded 
the operator+ method, but there is any easy to fix that in the docs, 
calling the python wrapper __add__ instead.

So far swig is my choice.  Boost.python looked good but I simple 
couldn't get bjam to work!

David




More information about the SciPy-User mailing list