Calling Haskell function from C (FFI) causes seg-fault (GHC,linux) 
Author Message
 Calling Haskell function from C (FFI) causes seg-fault (GHC,linux)

Hello,

I'm trying to figure out the murky depths of FFI calls from C to Haskell, using
the GHC compiler on Linux. Basically, the GHC docs on FFI are a bit sparse, so
I'm pretty sure I'm doing something wrong. Well, here's a summary of what I'm
doing:

Creating a Foo.hs file which contains a Foo module and a function "foo" which
returns a number (IO Int). I also have a test.c file which just tries to call
this function and print out the result. However, when I run the resulting
executable, I get a core dump (seg fault) from the call to Haskell. Any ideas?

The Haskell file (Foo.hs):

module Foo where

foreign export ccall foo :: Int -> IO Int

foo   :: Int -> IO Int
foo n = return n

---
The C-file (test.c):

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

int main()
{
    int res;

    res = foo(12);
    printf("res = %d\n", res);
    return 0;

Quote:
}

---

I compile all this like this (after a *long* time figuring out that I had to use
ghc to compile the c file too :):

ghc -o test Foo.hs test.c -fglasgow-exts

and get a resulting "test" executable, which cores when run. (BTW - anyone know
how to get ghc to compile with symbols enabled (equivalent to -g in gcc)?)

Any help greatly appreciated - I've searched the ghc docs and online tutorials,
but come up with nothing.

Neil.

PS - ghc version 5.04.1, SuSE Linux 8.1
--
package r Tkhtml;package r http;pack [scrollbar .v -o v -co {.h yv}] -s right\
-f y;pack [html .h -ys {.v set}] -f both -e 1;bind .h.x <1> {eval g [.h href %x\
%y]};proc g u {set t [http::geturl $u];.h cl;.h p [http::data $t];http::cleanup\
$t;.h co -base $u};g http://www.*-*-*.com/ ;proc bgerror args {};# NEM :-)



Thu, 12 May 2005 06:26:16 GMT  
 Calling Haskell function from C (FFI) causes seg-fault (GHC,linux)

Quote:

> Hello,

> I'm trying to figure out the murky depths of FFI calls from C to Haskell,
> using the GHC compiler on Linux. Basically, the GHC docs on FFI are a bit
> sparse, so I'm pretty sure I'm doing something wrong. Well, here's a summary
> of what I'm doing:

<snip...>

D'oh! Must have been too tired yesterday to notice the "Using your own main()
function" section in the docs....

Neil
--
package r Tkhtml;package r http;pack [scrollbar .v -o v -co {.h yv}] -s right\
-f y;pack [html .h -ys {.v set}] -f both -e 1;bind .h.x <1> {eval g [.h href %x\
%y]};proc g u {set t [http::geturl $u];.h cl;.h p [http::data $t];http::cleanup\
$t;.h co -base $u};g http://mini.net/tcl/976.html;proc bgerror args {};# NEM :-)



Thu, 12 May 2005 12:49:53 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. pdb causes seg. fault

2. recursive lists cause seg fault

3. pkg_mkIndex causes seg fault

4. empty list Tcl_Objs cause seg. fault

5. Help - f2c (linux) causes executable to fail with Seg Vi.

6. Linux Poplog - seg fault?

7. seg fault on tcl8.2 linux ppc R5 glibc2 kernel2.2.6-15apmac

8. Wish8.0 SEG faulting in linux

9. some clear examples for FFI call to a Windows api function in Franz Allegro for Windows

10. fort77 executable causes 'seg. fault' (glibc2 linux system)

11. Plug and Play Function Call Causes Lock up.

12. Plug and Play Function Call Causes Lock up.

 

 
Powered by phpBB® Forum Software