[plt-scheme] C++ Bindings using FFI

From: Hans Oesterholt-Dijkema (hdnews at gawab.com)
Date: Tue May 9 15:08:13 EDT 2006

> I've never used SWIG so this comment might be biased and brutally wrong.
> As far as I have understood libffi opens the library in scheme, reads
> the file and lets you in Scheme create bindings accessing its
> internals, so it seems to have a dynamic access to the library, if the
> library changes, the bindings change with it.
> In Swig, the header info given by the typemaps are passed to swig
> which parses the headers or whatever and generates the C code for
> mzscheme to generate the bindings for the library, which means that if
> the lib changes you have to rerun swig on the headers and typemaps to
> have new bindings, so it generates 'static bindings' in comparison
> with libffi.
> At first I would think that libffi would be the way to go. I see no
> reason for using swig now with libffi available.
Quite a statement. As far as I know, the FFI also needs to be told
what type a function in a library is. It would have to know type information
from the library to make the right calls to functions in the library.

In general it seems to me that the case of a changing interface to
a function in a general library will be problematic always, not only
to the scheme FFI or SWIG binding code. Imagine C programs
linked dynamically against libraries and the library being updated
to a new version with a changed function interface, uch!

For my mzgtk2 bindings I've patched the mzscheme generator.
It generates code, that resolves functions in libraries at runtime
(using e.g. 'dlopen' on unix). This makes it possible to generate
binding code from the latest header files of Gtk+, while still
being able to use the generated code with all versions of Gtk+2.x.
It complains only when functions cannot be resolved in older
Gtk+ libraries.


Posted on the users mailing list.