FFI: calling Haskell from C++?

Felix Breuer felix at fbreuer.de
Tue Sep 13 12:27:43 EDT 2005

Hello everyone,

the examples are working now! John, Wolfgang and Rich, thank you for your help!

On Wed, 14 Sep 2005 02:07:52 +1000
skaller <skaller at users.sourceforge.net> wrote:

> On Tue, 2005-09-13 at 15:20 +0000, Felix Breuer wrote:
> >   main.o(.text+0x22): In function `main':
> >   main.cpp: undefined reference to `__stginit_Foo()'

This was solved by

  extern "C" void __stginit_Foo(void);

> >   main.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
> >   collect2: ld returned 1 exit status

and this by adding -lstdc++ to the ghc command line options. My platform
is Linux 2.6.11 running GCC 3-4-4..

I will now try to apply this to my application. I will see if I run
into trouble when I actually use C++ code (and not just rename a C
source file). 

> There is one more problem: you MUST NOT run gcc on files
> ending in *.cpp. You are REQUIRED to run g++. So you will have
> to somehow tell ghc to use g++ and NOT gcc. in particular
> C++ programs require different startup code to C, to initialise
> exception handling, etc etc. So the whole program MUST be 
> linked by g++, not gcc, and the main() must be compiled with
> g++ not gcc. 

Is it possible to have ghc compile _and_ _link_ all the Haskell code,
and then using g++ to compile the C++ code and link everything together?

I tried using ghc to only compile the .hs files and have g++ do all the
linking but I only got a bunch of undefined references due to the GHC
libs not being linked into the executable. I always relied on ghc --make 
to figure out the dependencies here, and now I don't know which files
to link against...

Thanks again,

More information about the Glasgow-haskell-users mailing list