Wrapping C++ code for python.

Alexander Staubo nospam-alex at mop.no
Sat May 22 10:09:01 EDT 1999


In article <FBLB47.9Ar at world.std.com>, wware-nospam at world.std.com says...
> Alex (alex at somewhere.round.here) wrote:
> : Hi.  Could someone please point me to some examples of Gnu C++ code
> : wrapped up for Python?  I am finding the documents rather heavy going,
> : because they don't give any good examples.
> 
> I don't have any, being a C guy rather than a C++ guy, but you might
> want to look at Swig (www.swig.org), which automates the construction
> of interfaces between C/C++ and Python/Tcl/Perl/etc. If I recall, the
> handling of C++ is quite elegant and straightforward.

Elegant and straightforward, yes, but there are quite a few gotchas. For 
example, I've experienced a number of surprising access violation errors 
in Python when the SWIG wrapper code does not correctly wrap a function.

Another problem is that SWIG does not fully support the whole C++ syntax, 
so it will complain about many things ANSI C++, such as namespaces (but 
not, strangely, templates). There are several workarounds available, all 
involving having SWIG ignore the code in question.

It's a great tool, although writing directly for Python migth give you a 
significant performance gain; the wrapper code generated by SWIG seems 
quite substantial in size. Maybe somebody else can verify this.

-- 
Alexander Staubo             http://www.mop.no/~alex/
"Give me an underground laboratory, half a dozen atom smashers and a beautiful 
girl in a diaphanous veil waiting to be turned into a chimpanzee, and I care 
not who writes the nation's laws." --S. J. Perelman




More information about the Python-list mailing list