problems exporting haskell functions

Simon Marlow simonmar@microsoft.com
Fri, 14 Sep 2001 17:05:12 +0100


This is a multi-part message in MIME format.

------_=_NextPart_001_01C13D37.091864B6
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

Hi there,

> Hello, I have tried to export haskell functions, but I=20
> haven't been able to compile anything and obtain a executable=20
> file. My program has two parts.=20
>  - Haskell part:
>       module Hex where
>       fh :: Int -> Int
>       foreign export "f"  fh :: Int -> Int
>       fh x =3D 7   =20
>=20
>  - C part:
>       #include <stdio.h>
>       #include <stdlib.h>=20
>       extern int f(int x);
>=20
>       int main( ){
>       int a;
>       a =3D f(5);
>       printf("%d\n",a);
>       return 0;
>       }

When you provide your own main() function, you have to arrange to
initialize GHC's runtime system.  I've attached some example code, and
I've added some material to the User's Guide for the next release which
explain how to do this.

Cheers,
	Simon

------_=_NextPart_001_01C13D37.091864B6
Content-Type: application/octet-stream;
	name="ffi002_c.c"
Content-Transfer-Encoding: base64
Content-Description: ffi002_c.c
Content-Disposition: attachment;
	filename="ffi002_c.c"

I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlICJmb29fc3R1Yi5oIgoKI2luY2x1ZGUgIlJ0c0FQ
SS5oIgoKZXh0ZXJuIHZvaWQgX19zdGdpbml0X0ZvbyAoIHZvaWQgKTsKCmludCBtYWluKHZvaWQp
CnsKICBpbnQgaTsKCiAgY2hhciogYm9ndXNGbGFnc1sxXSA9IHsgIlwwIiB9OwoKICBzdGFydHVw
SGFza2VsbCgwLCBib2d1c0ZsYWdzLCBfX3N0Z2luaXRfRm9vKTsKCiAgZm9yIChpID0gMDsgaSA8
IDU7IGkrKykgewogICAgcHJpbnRmKCIlZFxuIiwgZm9vKDI1MDApKTsKICB9CgogIHNodXRkb3du
SGFza2VsbCgpOwoKICByZXR1cm4gMDsKfQo=

------_=_NextPart_001_01C13D37.091864B6
Content-Type: application/octet-stream;
	name="ffi002.hs"
Content-Transfer-Encoding: base64
Content-Description: ffi002.hs
Content-Disposition: attachment;
	filename="ffi002.hs"

bW9kdWxlIEZvbyB3aGVyZQoKZm9yZWlnbiBleHBvcnQgZm9vIDo6IEludCAtPiBJTyBJbnQKCmZv
byA6OiBJbnQgLT4gSU8gSW50CmZvbyBuID0gcmV0dXJuIChsZW5ndGggKGYgbikpCgpmIDo6IElu
dCAtPiBbSW50XQpmIDAgPSBbXQpmIG4gPSBuOihmIChuLTEpKQoK

------_=_NextPart_001_01C13D37.091864B6--