ffi howto

jacob colvin carbonated_beef at hotmail.com
Tue Jul 30 05:47:32 EDT 2002

Hi, I've been trying to get FFI to just do something, anything, for the past 
few weeks and have not been able to do anything.  I've looked around for 
documentation and have read the ghc's documention page for ffi several times 
and nothing works.  Main problem, I think, is that I am not sure how I 
should compile the *.hs and *.c files.

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


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.

my best linux output...
[Jacob at localhost ffi]$ ls
main.c  M.hs
[Jacob at localhost ffi]$ ghc -fglasgow-exts M.hs
/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'
main.c: In function `main':
main.c:12: `__stginit_foo' undeclared (first use in this function)
main.c:12: (Each undeclared identifier is reported only once
main.c:12: for each function it appears in.)
main.c: At top level:
/usr/lib/ghc-5.04/include/RtsAPI.h:125: storage size of 
`GHCziTopHandler_runIO_closure' isn't known
/usr/lib/ghc-5.04/include/RtsAPI.h:126: storage size of 
`GHCziTopHandler_runNonIO_closure' isn't known
[Jacob at localhost ffi]$ ls
Foo.hi  main.c  M.hs  M.o  M_stub.c  M_stub.h  M_stub.o
[Jacob at localhost ffi]$

Not sure if this is the apropriate mailinglists, but I have looked in most 
of the others, and FFI is rarely discussed.

Thank you for listening,
Jacob Colvin

Join the world’s largest e-mail service with MSN Hotmail. 

More information about the FFI mailing list