FFI: calling Haskell from C++?

Felix Breuer felix at fbreuer.de
Tue Sep 13 11:20:44 EDT 2005

Hello everyone,

we have tried long and hard to call Haskell functions from C++ using
the FFI mechanism but without success. Our problem is as follows:

We tried the C case first. Taking the example code from Chapter 
8.2 of the GHC User Guide, we created files Foo.hs and main.c
and then did

  $ ghc -fffi -c Foo.hs
  /tmp/ghc28407.hc: In function `__stginit_Foo_':
  /tmp/ghc28407.hc:278: warning: implicit declaration of function `getStablePtr'
  $ ghc -fffi Foo. 
  Foo.hi  Foo.hs  Foo.o   
  $ mv main.cpp main.c   
  $ ghc -fffi Foo.o Foo_stub.o  main.c 
  $ ./a.out 

which worked fine.

But when we simply rename main.c  ->  main.cpp, then

  $ 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

We tried many different options to ghc and also using g++
directly, all to no avail. It would be really great if someone could shed
some light on this. For convenience, we have attached Foo.hs and main.cpp.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Foo.hs
Type: application/octet-stream
Size: 158 bytes
Desc: not available
Url : http://www.haskell.org//pipermail/glasgow-haskell-users/attachments/20050913/3169d708/Foo.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: main.cpp
Type: application/octet-stream
Size: 393 bytes
Desc: not available
Url : http://www.haskell.org//pipermail/glasgow-haskell-users/attachments/20050913/3169d708/main.obj

More information about the Glasgow-haskell-users mailing list