FFI: calling Haskell from C++?

Simon Peyton-Jones simonpj at microsoft.com
Wed Sep 14 11:05:26 EDT 2005


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 at haskell.org
[mailto:glasgow-haskell-users-bounces at haskell.org] On Behalf Of Rich
Neswold
Sent: 13 September 2005 16:45
To: Felix Breuer
Cc: glasgow-haskell-users at haskell.org
Subject: Re: FFI: calling Haskell from C++?

 

On 9/13/05, Felix Breuer <felix at 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 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org//pipermail/glasgow-haskell-users/attachments/20050914/5f9c90be/attachment-0001.htm


More information about the Glasgow-haskell-users mailing list