[GUI] Example: Binding C++ to Haskell using the ffi

Daan Leijen daanleijen@xs4all.nl
Sat, 01 Mar 2003 23:11:55 +0100

On Sat, 1 Mar 2003 13:40:42 -0800 (PST), David Sankel <camio@yahoo.com> wrote:

> Has any though gone into cplusplus?  If so, how would
> it interface?  Would stubs on the c++ side be
> generated or would it use the code directly.

I think that it is in general infeasable to call c++ member
function directly as different c++ compilers use wildly different
calling conventions and name-mangling schemes. It makes more sense
to call C++ via extern "C" wrappers and use foreign import ccall on
the Haskell side.

In my opinion, the 'ideal' way of interfacing to C++, or C, would
be an adoption of H/Direct to read C or C++ header files directly
and translating it to a default IDL specification -- one should
than be able to augment this specification with IDL attributes,
much like the current 'asf' files of H/Direct but probably more friendly. H/Direct could than generate both exern "C" wrappers and
Haskell marshalling code at the same time.

On the other hand, even though the above approach would solve all
the interface problems once and for all, it certainly involves a significant
amount of work to extend H/Direct with a C++ front-end and better asf support.

All the best,

ps. I have been experimenting with interfacing to the wxWindows
library, a large C++ framework. I have used an existing Eiffel library
that wrapped the C++ classes in C functions together with a small
hand-written tool to translate their (simple) header files to Haskell
marshalling code -- a poor-mans solution until H/Direct gets an update :-)
It works rather well and imports 400 classes with 2500 methods with
very reasonable type signatures. You may want to look at it to get some
inspiration for interfacing to C++: http://wxhaskell.sourceforge.net
It also shows how you can use polymorphism to mimic inheritance
relationships (as described in "Calling hell from heaven and heaven
from hell")
pps. This is just a preview, don't expect a wxHaskell too soon, I have
a thesis to finish in the next weeks :-)

> Later,
> David J. Sankel
> _______________________________________________
> GUI mailing list
> GUI@haskell.org
> http://www.haskell.org/mailman/listinfo/gui