ffi howto

Simon Marlow simonmar at microsoft.com
Tue Jul 30 08:47:03 EDT 2002


> 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
it.

Cheers,
	Simon



More information about the FFI mailing list