C main

Sigbjorn Finne sof@microsoft.com
Thu, 12 Oct 2000 10:17:35 -0700

Simon Marlow writes:
> > 
> > Could someone instruct me on (or point me to) the correct ghc 
> > command line for calling haskell from C?
> > ...
> You can use ghc just like you would use gcc to compile up 
> your C program,
> then use ghc again to link the whole program with the Haskell 
> module(s). The -no-hs-main flag shouldn't be necessary, as
> far as I can tell. eg.
> 	$ ghc -c main.c
> 	$ ghc -c HaskellStuff.hs
> 	$ ghc main.o HaskellStuff.o

That will (sort of) work if you also insist that HaskellStuff.hs
implements Main.main. As the name suggests, -no-hs-main takes care
of the case when HaskellStuff.hs doesn't. (Well almost, the 4.08 driver
contains a bug in setupLinkOpts(); __init_PrelMain should be handled
the same way as PrelMain_mainIO_closure. )

In either case, you need to make sure that the RTS is started up,
since you're not using the RTS-supplied main(). The HDirect distribution
contains an example of this in examples/server/, but notice that the
4.08 RTS' startupHaskell() signature has changed to also take an extra
'module root' argument. However, even with that change, 4.08
compiled code crashes when running (4.05 works fine), which leads
me to believe there's something else going on here :-) This is on
the same platform that Brett originally reported the problem on.


btw, 4.05 contained a bug in this area which made -no-hs-main only
work when linking with -static. 4.08 seems to have inherited it, i.e.,
in prepareWin32DllLink(), the condition for pushing Main.dll_o and
PrelMain.dll_o onto @Link_file should be ($linking_main && !$NoHaskellMain)
and not just $linking_main