Cxx foreign function interface

From HaskellWiki
Revision as of 21:25, 13 May 2017 by Henk-Jan van Tuyl (talk | contribs) (Difficulties of Interfacing to C++: Updated the link to the FFI specification)

Jump to: navigation, search

[To do: link here from other pages?]

This page outlines the challenges involved in building a foreign function interface (FFI) from Haskell to C++ libraries, beyond the usual difficulties of FFI, and then describes some (possible) solutions to the difficulties presented by C++.

FFI Background


Difficulties of Interfacing to C++


  3. Inheritance (maybe)
  4. Template functions, template classes, etc.

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 bridging the gap between Haskell and C++.
  • Hacanon automatically generates an FFI to C++ using Template Haskell, but its author has placed it "on hold" and sees some inherent flaws in using Template Haskell. (See David Himmelstrup, 2006, "About Lemmih", accessed 20 June 2009)
    • ZeroTH (zeroth) fixes the problems of Hacanon and Hacanon-light. ZeroTH by itself only removes Template Haskell dependencies from Haskell programs; so, you could use either (Hacanon + ZeroTH) or (Hacanon-light + ZeroTH), but not ZeroTH by itself, to generate C++ bindings.
  • fficxx fficxx (“eff fix”) is an automatic haskell Foreign Function Interface (FFI) generator to C++.
  • Hoppy generates bindings to C++ code from API descriptions written in Haskell, and supports exceptions, (limited) templates, callbacks, and garbage collection. Basic C++ standard library bindings are also included.
  • SWIG (Simplified Wrapper and Interface Generator) can be used to connect C and C++ with many languages, such as Python, Tcl, Java, several versions of Lisp, and OCAML), but alas, not Haskell. Or at least not directly. But with MissingPy, which provides access to the Python interpreter, it might be possible to do this indirectly: first, make the C++ library accessible to Python (already done, in many cases), then, let your Haskell program use the Python interpreter to make calls into the C++ library. Maybe this is also possible with some other scripting languages.