Help in calling Haskell from C

Sigbjorn Finne sof@galconn.com
Tue, 14 Aug 2001 11:28:57 -0700


This is a multi-part message in MIME format.

------=_NextPart_000_0177_01C124B4.4E9279A0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hi,

the 0.17 documentation (and examples) wasn't updated to cover
the extra argument that startupHaskell() now takes, I'm afraid.
Attached is a version of tst.c from examples/server/ which shows
you how to now use startupHaskell() from C.

hth
--sigbjorn

btw, if you experience GHC5 compatibility problems with
HDirect-generated code, checking out a copy of the HDirect sources
from the CVS repository is a good idea. And if that doesn't clear
up  the problems you're having either, then please let me know & I'll
try to sort it out.

----- Original Message ----- 
From: "Mark Conway Wirt" <mark@intrepid.net>
To: <glasgow-haskell-users@haskell.org>
Sent: Tuesday, August 14, 2001 08:49
Subject: Help in calling Haskell from C


> I've been using hdirect for C <-> Haskell calls, and I've hit a bit
> of a snag.  I'm using ghc 5.00.2 and hdirect 0.17.
> 
> I've been able to call Haskell from C easily enough, but I've been
> unable to go the other way.
> 
> I've tried following the directions in the hdirect manual, and tried
> building the examples in the hdirect distribution.  Everything works
> up until final linking, where I receive undefined references.
> 
> The C code should initialize the Haskell run-time, as it include the
> startup code:
> 
>     extern void startupHaskell (int argc, char* argv[]);
>     startupHaskell(argc,argv);
> 
> I'm compiling via ghc with the following options:
> 
>     ghc -package hdirect -fglasgow-exts -static -no-hs-main 
> 
> and the idl is compiled as such:
> 
> ihc -s -fhs-to-c --gen-headers -fuse-ints-everywhere --output-h=....
> 
> Does hdirect work with ghc 5.00.2?  I'm not receiving any errors in the
> build process, so I think my problems are more related to a lack of
> documentation, as opposed to a bug.  Does anyone have any information
> (beyond what is in the hdirect documentation) on calling ghc-compiled
> code from C?
> 
> The undefined references I'm getting are:
> 
>      undefined reference to `__init_Main'
>      undefined reference to `Main_main_closure'
> 
> 
> Thanks in advance.  I've looked though the mailing list archive and
> haven't found anything.  If this has been answered before I apologize.
> 
> --Mark
> 
> 
> _______________________________________________
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users@haskell.org
> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users

------=_NextPart_000_0177_01C124B4.4E9279A0
Content-Type: application/octet-stream;
	name="tst.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="tst.c"


#include <stdio.h>
#include "MathLib.h"

#if __GLASGOW_HASKELL__ >= 408
extern void startupHaskell (int argc, char* argv[], void* rootMod);
extern void* __init_MathLibProxy;
#else
extern void startupHaskell (int argc, char* argv[]);
#endif
int
main(int argc, char* argv[])
{
  /* Initialise the GHC run-time system */
#if __GLASGOW_HASKELL__ >= 408
  startupHaskell(argc,argv,&__init_MathLibProxy);
#else
  startupHaskell(argc,argv);
#endif

  printf("nfib(10) = %d\n", nfib(19));
  printf("fac(%d)  = %d\n", 5, fac(5));
  return 0;
}

------=_NextPart_000_0177_01C124B4.4E9279A0--