There’s been a bit of traffic about calling Haskell from C++ recently, and I know that calling C++ from Haskell is a frequently asked question. (E.g. Koen Claessen was asking me about it this week.)  It also seems to be somewhat platform dependent.  (Esp on Windows, I think.)

 

          Would someone like to write up a description of how

          to successfully call out from Haskell to C++ and vice versa,

          using GHC? 

 

If you did, we’d gladly include the result in the user manual, and save others a lot of head scratching.  Maybe there are simple things we could do to GHC to make the process simpler.  But what it needs is someone who knows C++ well to write a coherent story.  Please!

 

Simon

 

 


From: glasgow-haskell-users-bounces@haskell.org [mailto:glasgow-haskell-users-bounces@haskell.org] On Behalf Of Rich Neswold
Sent: 13 September 2005 16:45
To: Felix Breuer
Cc: glasgow-haskell-users@haskell.org
Subject: Re: FFI: calling Haskell from C++?

 

On 9/13/05, Felix Breuer <felix@fbreuer.de> wrote:

  $ ghc -fffi Foo.o Foo_stub.o main.cpp
  main.o(.text+0x22): In function `main':
  main.cpp: undefined reference to `__stginit_Foo()'
  main.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
  collect2: ld returned 1 exit status


In main.cpp, you need to define __stginit_Foo(void) like this:

    extern "C" void __stginit_Foo(void);

Specifying the "C" linkage with prevent the compiler from looking for a name-mangled version of the function.

To fix the second problem, you need to shut off exception handling (specifying the -fno-exceptions option ought to do this. I don't know how to pass compiler option via ghc, though.

--
Rich

AIM : rnezzy
ICQ : 174908475