1 Abstract

This page describes some of the black magic needed to call C++ from Haskell (on linux, compiled with g++).
I refer to it as the "The Hard Way" because it is a tedious, hand done method of generating C++ bindings. The information
on this page is pretty spotty and probably wrong in places. But hopefully it will be of some use. You may also want to look at
Hacanon. It is currently unmaintained and needs a rewrite, but the idea of using Template Haskell is pretty appealing.

gcc-xml might be a good way to get the information needed to do more automatic C++ binding generation.

(BTW, this was written by JeremyShaw, know as stepcut on irc.freenode.net/#haskell if you have questions).

2 The basic steps

There are two phases to calling C++ from Haskell

making the objects accessible to Haskell programs

creating objects at run-time

2.1 Making objects accessible to Haskell

According to "The Haskell 98 Foreign Function Interface 1.0" report -- you import a C++ function by simply specifying the calling convention cplusplus. Unfortunately, no haskell compiler actually supports this calling convention, so we have to use the C calling convention instead.

The standard method for dealing with this is to write C++ code that uses extern "C" to export the methods unmangled. Of course, you still have to uniquify the method names, but *you* get to pick the names instead of letting the compiler do whatever wacky method it wants.

The method presented on this page does not use that method -- it just calls the mangled names directly. So far this seems to work ok...

2.1.1 Finding the mangled named

C++ allows you to have many methods with the same name, but different arguments. However, the underlying linker does not --
so C++ mangles the methods names to ensure that each method name is unique. To import these methods into haskell, we
need to know the mangled name. Since there is no standard for mangling names, we must figured out how our C++ compiler
decides to do things.

All the methods in the KApplication class take a pointer to KApplication as their first argument. Normally C++ manages
this for you -- so you never see it. The pointer is, of course, the 'this' pointer.

2.1.3 Calculating the object size

We also need to know how big the object is, so we can allocate space for it with new() later.

2.3 How to link the program

Most programming languages, especially Haskell and C++, link by calling the system linker, passing it appropriate libraries. If you link Haskell and C++ code into the same binary, you cannot have this convenience for both languages. Also, one of both runtime systems may not get initialized correctly.

Currently, I'm using Ghc to do the linking, and instruct it to link the c++ runtime library:

This seems to work (with Ghc 6.4.1 and G++ 3.3.5). However, I'm pretty certain, the C++ runtime doesn't get initialized, so as soon as someone touches, say {{{std::cin}}}, all hell breaks lose. I dare not think about what happens, should I ever feel the need to bring a third language into the mix.

AnswerMe: Can anyone confirm that this works, whether it is supposed to work and how to do this correctly? -- UdoStenzel

2.4 An example

Here is an example where I create enough bindings to launch a small KDE app.
This is actually a bad example because it has a lot of stuff that is unrelated to C++ bindings.
Also, it does some things wrong. Things I know of include:

* Should probably use Finalizers and ForeignPtr's
* I read somewhere that you need to call the C++ main() to ensure that some C++ stdlib stuff is properly initialized.

Also of note is the ->> operator. The entire purpose of the operator is to make things look more C++ like.

instead of writing:

kapplication_KApplication kapp 11

I can use the more C++ like syntax:

(kapp->> kapplication_KApplication)11

Though, I think I actually prefer the former.

To compile the code on a debian system you need:

ghc6 and the kdelibs4-dev

My kdelibs is version 3.3.2 and was compiled with the g++ 3.x compiler.
I think the name mangling scheme changed with g++ 4.0, so if you are using that version
you will have to figure out the new mangled names.

Also, I don't believe the buttons are actually hooked up yet. I think it just sits there looking pretty.