[Geowanking] c++/python binding wisdom

Howard Butler hobu.inc at gmail.com
Thu May 22 10:13:06 PDT 2008

On May 22, 2008, at 11:48 AM, Nino Walker wrote:

> Sorry for the ambiguity: the codebase is C++, and there is no C  
> interface to it.
> Nino
> -----Original Message-----
> From: geowanking-bounces at lists.burri.to [mailto:geowanking-bounces at lists.burri.to 
> ] On Behalf Of Mateusz Loskot
> Sent: Thursday, May 22, 2008 8:43 AM
> To: geowanking at lists.burri.to
> Subject: Re: [Geowanking] c++/python binding wisdom
> On May 22, 2008, at 5:18 PM, nino at urbanmapping.com wrote:
>> Hopefully, y'all with experience writing or using swig bindings or
>> other c/py framework can weigh in.  Rants, gotchas, resources,
>> whatever great or token wisdom will be sincerely appreciated.
> In the subject you use C++/Python but above you use C/Python, both are
> very different cases. If you wrap C++ API, I'd go with Boost Python
> [1]. IMHO, there is nothing better than Boost Python for C++, it beats
> SWIG regarding C++ language support.
> I don't have much experience with wrapping C for Python, but some
> users say ctypes may be better than SWIG.
> [1] http://www.boost.org/doc/libs/1_35_0/libs/python/doc/index.html

SWIG sucks for a number of reasons and is great for exactly one reason  
-- it support more than one language.  If you need to support bindings  
for more than one language *right now,* I would look at using SWIG.   
Otherwise, I would skip it.  (Speaking as a SWIG bindings developer  
for GDAL and MapServer).

Writing Python bindings against a C++ API can be problematic because  
of ABI hell.  Small changes/additions/deletions/const correctness  
issues in subsequent C++ library releases will make your bindings fail  
to work without recompiling them.  This means that the bindings have  
to be built and ship with the library itself rather than being made  
available through something like PyPi.  Python's easy_install and ilk  
can make it really simple for folks to get going with bindings if they  
are getting the base libraries through their package manager, but this  
only works if ABI issues aren't in play.

A C API + ctypes (foreign function interface standard in Python 2.5+)  
is a really nice way to go.  ABI issues tend to go away, most people  
have ctypes installed already or can get it quickly, and you can leave  
a number of hassles at the feet of the ctypes developers (cross  
platform issues, the need for compiling bindings code on windows).


More information about the Geowanking mailing list