Linking to Haskell code from an external program

Simon Peyton-Jones simonpj at microsoft.com
Mon Nov 24 04:00:11 EST 2008


It looks as if you are somehow failing to link your binary with package 'base'. (Are you using 'ghc' as your linker; you should be.)  But others are better than I at this kind of stuff.

Simon

| -----Original Message-----
| From: glasgow-haskell-users-bounces at haskell.org [mailto:glasgow-haskell-users-
| bounces at haskell.org] On Behalf Of Colin Paul Adams
| Sent: 23 November 2008 15:20
| To: Simon Marlow
| Cc: glasgow-haskell-users at haskell.org
| Subject: Re: Linking to Haskell code from an external program
|
| >>>>> "Colin" == Colin Paul Adams <colin at colina.demon.co.uk> writes:
|
| >>>>> "Simon" == Simon Marlow <marlowsd at gmail.com> writes:
|     Colin> I then attempted to move it into Eiffel. I can compile the
|     Colin> C code OK, but I'm running into problems with linking.
|     >>>
|     Colin> I solved most of the problems by adding the -v flag to the
|     Colin> call to ghc which I used to link the original (haskell + c
|     Colin> only) program, and cut-and-paste the linker options from
|     Colin> their into the Eiffel configuration file. This isn't really
|     Colin> satisfactory - I would like some automatic way to determine
|     Colin> what the flags should be.
|
|     Simon> The only other way I can think of is to construct the
|     Simon> arguments yourself by querying the package database,
|     Simon> e.g. "ghc-pkg field ld-options rts", but you'll have to
|     Simon> combine the information from several different fields of
|     Simon> the packages you use, and basically reproduce what GHC does
|     Simon> to construct the ld command line.
|
| I have upgraded to ghc 6.10.1 by compiling from source.
|
| Now I can't get it to link, either using my previous method of linking
| a standalone C program via ghc -v, and copying the -u options, or by
| running:
|
| ghc-pkg field rts ld-options
|
| (I found out by trial-and error that the syntax you gave above was
| inverted).
|
| In both cases, I get errors:
|
| /home/colin/fib/Fib.o: In function `r131_info':
| (.text+0x79): undefined reference to `base_GHCziBase_ZC_con_info'
| /home/colin/fib/Fib.o: In function `r131_info':
| (.text+0x10f): undefined reference to `base_GHCziBase_ZMZN_closure'
| /home/colin/fib/Fib.o: In function `s13p_info':
| (.text+0x157): undefined reference to `base_GHCziBase_ZMZN_closure'
| /home/colin/fib/Fib.o: In function `s13F_info':
| (.text+0x2cf): undefined reference to `base_GHCziBase_ZMZN_closure'
| /home/colin/fib/Fib.o: In function `s13c_info':
| (.text+0x32): undefined reference to `base_GHCziNum_plusInteger_info'
| /home/colin/fib/Fib.o: In function `r131_info':
| (.text+0x124): undefined reference to `base_GHCziList_foldr21_info'
| /home/colin/fib/Fib.o:(.data+0x0): undefined reference to `base_GHCziNum_Szh_static_info'
| /home/colin/fib/Fib.o:(.data+0x10): undefined reference to `base_GHCziNum_Szh_static_info'
| /home/colin/fib/Fib.o: In function `r131_closure':
| (.data+0x38): undefined reference to `base_GHCziBase_ZC_static_info'
| /home/colin/fib/Fib.o: In function `r131_closure':
| (.data+0x58): undefined reference to `base_GHCziBase_ZC_static_info'
| collect2: ld returned 1 exit status
|
| I tried in desperation to add -u flag for each of the undefined
| symbols above, but it makes no difference.
|
| I'm quite lost now.
|
| My C code looks like this:
|
| static char * program_name = "Eiffel program";
| static char * terminator = (char *) 0;
| int argc;
| char **argv[2];
| argc = 1;
| argv [0] = &program_name;
| argv [1] = &terminator;
|  hs_init(&argc, argv);
| #ifdef __GLASGOW_HASKELL__
| #include "Fib_stub.h"
| extern void __stginit_Fib ( void );
|   hs_add_root(__stginit_Fib);
| #endif
|               return (fibonacci_hs($n));
| hs_exit();
|
| (and in addition there are includes for <stdio.c> and "HsFFI.h"
| somewhere preceding the above).
| --
| Colin Adams
| Preston Lancashire
| _______________________________________________
| Glasgow-haskell-users mailing list
| Glasgow-haskell-users at haskell.org
| http://www.haskell.org/mailman/listinfo/glasgow-haskell-users



More information about the Glasgow-haskell-users mailing list