Cxx foreign function interface

From HaskellWiki
Revision as of 01:40, 20 June 2009 by Gdweber (talk | contribs) (First draft)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Foreign Function Interfaces to C++ Libraries

Foreign function interfaces (FFIs) from Haskell to C are well developed, but writing an FFI to a C++ library remains a difficult undertaking. In general, the Haskell FFI apparatus specifies how to call a function (not a method) using the "ccall" calling mechanism used for C programs. The FFI specification (** need link) mentions, but does not require, an alternative calling mechanism for C++; no Haskell compiler has ever implemented this. Extra complications arise in interfacing to C++ libraries:

  1. A C++ program allows overloading, using the same function name with different argument lists. But at the binary level, each different version of the function must have its own name. The C++ compiler "mangles" the function names to differentiate them.
  2. A C++ program may have classes with constructors, destructors, and methods. These are, obviously, not quite the same as functions, and have no obvious counterpart in Haskell.
  3. Template functions, template classes, etc.

Here are some approaches to building interfaces to C++:

  • IO_inside describes how to interface to a C++ library using the "export C" declaration (in C++) which makes it look like a C library. This seems to be the standard technique for briding the gap between Haskell and C++.
  • CPlusPlus_from_Haskell describes "the hard way" of interfacing to C++, using the mangled C++ names directly without using "export C". It also suggests the possibility of a more automated approach using gcc-xml.
  • Hacanon automatically generates an FFI to C++ using Template Haskell (?), but it is unmaintained and its author seems dissatisfied with it.
    • Need link
  • The HQK project reports having developed a small Haskell module, qoo, for accessing object-oriented libraries conveniently.