[plt-scheme] C++ Bindings using FFI

From: Eli Barzilay (eli at barzilay.org)
Date: Sat May 6 22:09:03 EDT 2006

On May  6, Hans Oesterholt-Dijkema wrote:
> See attached SWIG setup code for mzgtk2.
> [...]

On May  6, Hans Oesterholt-Dijkema wrote:
> > Don't understand how to use both if like Eli said, they both seem
> > to do the same.
> >
> SWIG is a parser and a generator. The current generator generates C
> glue code. Instead of generating C glue code, FFI (i.e. scheme) glue
> code could be generated.

1. The SWIG code that you sent doesn't do anything with the Scheme
   foreign interface.

2. It won't help you deal with the difficulties of interfacing C++

In any case -- yes, it's a good idea to use SWIG to generate the
Scheme interface description -- and it would even be better to do it
in Scheme, given a C header parser.  (Joe Marshall had a working
parser if anyone's interested in taking that.)  In any case, you can
either approach this dynamically and create the interface at run-time,
or statically and distribute a pre-made interface.

SWIG will work for a pre-made interface, as a number of other
approaches, including parsing header files, or the SGL approach of
compiling and running a small C file that spits out the required
information (mostly integer sizes).  The problem is that all of these
require you to distribute a fixed interface, which might break on user
machines that have different setups.

The dynamic approach is much better, but things become more difficult
-- you probably won't have SWIG on the user machine, possibly not even
GCC.  Even if you can parse header files, you may not have them...
There's no simple way around this without the required
meta-information...  It's the basic problem of living in a world of
meta-information-less C libraries.

There are some libraries, like ImageMagick, that are really good for
foreign interfaces.  In the ImageMagick case, the library can have a
different-sized integer as the basic type that is used -- and you get
a function in the interface that returns this size, which makes it
easy to bind it dynamically.

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.