CXX and Swig, pros and cons

David Abrahams abrahams at mediaone.net
Thu Jul 20 12:15:46 EDT 2000


"Roger Hansen" <rogerha at ifi.uio.no> wrote in message
news:ya0em4qffs4.fsf at midgard.ifi.uio.no...
> * Jérôme Lecomte
> >
> > I am fairly new to Python and I'd like to interface part of a medium
> > size C++ lib (around 70 classes) to Python. I looked on the www and
> > I found CXX and Swig. They seem to have fairly different approach of
> > the problem. I wonder which suits best C++.
>
> That depends! SWIG is very good with C, but not with advanced C++,
> that is templates, STL, etc. So if the library uses simple C++
> features, you could use SWIG. The nice thing with SWIG is that it can
> be very simple to build a good interface. Read the C++ support chapter
> in User's Guide to determine if you can use SWIG.
>
> CXX has as you say a very different approach. I've just looked at it
> myself and are struggling a bit. But I do like some of the features,
> like the nice connection to STL.
>
> > If I miss any other tool, please let me know.
>
> SILOON which you can find at <URL: http://www.acl.lanl.gov/siloon/ >
> looks promising. I haven't tried it yet though.

There's another one which I've been developing called "py_cpp" at
http://www.egroups.com/files/boost/py_cpp/.
[I'm thinking of changing the name to "Raymond Luxury Yacht" - can someone
come up with a better moniker?]
If you're not a boost mailing list member you may need to join in order to
be able to look at this page. Let me know if you have trouble getting there.

The system is still lacking formal documentation, but it does include some
fairly complete and well-commented usage examples. Look at extclass_demo.*

Advantages of py_cpp include:
1. Automatic conversion and type-checking of argument lists. You should
never have to write PyArg_ParseTuple() (or the equivalent) again.
2. You can subclass your C++ classes in Python and override their virtual
functions
3. C++ objects held by smart pointers (e.g. std::auto_ptr,
boost::shared_ptr) look just like the corresponding object in Python -
indirection is "flattened".
4. In other respects you should be able to replicate your C++ interface in
Python with little or no intrusion into your C++ design and very little
support code.

This has been tested with MSVC6.3 with and without the STLport library, and
with GCC 2.95.2 using STLport. The project requires the presence of the free
boost libraries (www.boost.org).

py_cpp is under active development and I intend to be responsive to bug
reports and requests.

-Dave





More information about the Python-list mailing list