ffi howto

jacob colvin carbonated_beef at hotmail.com
Tue Jul 30 21:01:33 EDT 2002

>From: "Simon Marlow" <simonmar at microsoft.com>
>To: "jacob colvin" <carbonated_beef at hotmail.com>,<ffi at haskell.org>
>Subject: RE: ffi howto
>Date: Tue, 30 Jul 2002 13:47:03 +0100
> > If someone could post an example of a "hs" and a "c" file and
> > how to compile
> > it with ghc, i would be very apreciative.  I have looked at
> >
> > http://www.haskell.org/ghc/docs/latest/html/users_guide/sec-ff
> > i-ghc.html
> >
> > for a long time, and I can't figure out how to reproduce the
> > example.  I
> > have noticed that if you name your hs file "M.hs" as the page
> > says, then ghc
> > will produce M_stub.h instead of the included "foo_stub.h"
> > that the c main
> > program lists.  I've used windowsXP and ghc[rpm] installed on
> > redhat7.3, and
> > still nothing works.
>The stub files are named after the original .hs file, so if you have
>Foo.hs you'll get Foo_stub.c.  Most people name their files after the
>module name, so for example Foo.hs contains module 'Foo' (in fact this
>is necessary if you want to use ghc --make or GHCi).
> > my best linux output...
> > [Jacob at localhost ffi]$ ls
> > main.c  M.hs
> > [Jacob at localhost ffi]$ ghc -fglasgow-exts M.hs
>I think you want 'ghc -c -fglasgow-exts M.hs' here.  Or maybe 'ghc
>-fglasgow-exts main.c M.hs' to do compilation and linking in one go.
> > /usr/lib/ghc-5.04/libHSrts.a(Main.o): In function `main':
> > Main.o(.text+0x4): undefined reference to `__stginit_Main'
> > Main.o(.text+0x1b): undefined reference to `Main_zdmain_closure'
> > collect2: ld returned 1 exit status
> > [Jacob at localhost ffi]$ ghc -fglasgow-exts main.c M_stub.c
> > In file included from main.c:4:
> > /usr/lib/ghc-5.04/include/RtsAPI.h:125: syntax error before `const'
> > /usr/lib/ghc-5.04/include/RtsAPI.h:126: syntax error before `const'
>This is a bug in 5.04; #include "Rts.h" before RtsAPI.h to work around
>	Simon

I finally got everything working, thanks for your help.  Including Rts.h 
before RtsAPI.h in main.c was the main problem.  Also i needed to include 
the Foo.o in addition to Foo_stub.o when I did the final linking.  For 

ghc -fffi -c Foo.hs
ghc -fffi Foo.o Foo_stub.o main.c

.. and once again, life is good.

Jacob Colvin

MSN Photos is the easiest way to share and print your photos: 

More information about the FFI mailing list