[Haskell] Building a dynamic loadable Parsec parser

Donald Bruce Stewart dons at cse.unsw.edu.au
Mon Feb 28 18:36:37 EST 2005

> Hi,
> I've got a little parser written using Parsec that I want to link into
> some C code.  I start by compiling the Haskell sources like so:
> 	ghc -ffi -fglasgow-exts -main-is My_Init -c parse.hs
> When linking parse.o and parse_stub.o against my (additional) glue code,
> I get the following errors:
> 	Main.o(.text+0xc): undefined reference to `__stginit_ZCMain'
> 	Main.o(.text+0x14): undefined reference to `__stginit_ZCMain'
> 	Main.o(.text+0x20): undefined reference to `ZCMain_main_closure'
> 	Main.o(.text+0x24): undefined reference to `ZCMain_main_closure'
> I'm trying to build a .so that I can load into another process (via Tcl,
> actually).  If I define a main function in my C glue code, these errors
> go away.  But I can't have a main function in this .so.
> So, two questions:
> 	- is it possible to compile and link these .o files into a .so
> 	  (on Solaris, using ghc 6.2.2)?
> 	- what do I need to do to create an entry point for this Haskell
> 	  code that _isn't_ called main()?

I think that unless you use the -PIC stuff that Wolfgang committed
recently then you'll have to instead use hs-plugins to do the dynamic
loading on the Haskell side. I'm fairly certain the pic stuff won't work
on Solaris.

Essentially, you write a Haskell wrapper module over the parser that
calls hs-plugins to load your parser. Then, using the FFI, expose a hook
to this wrapper module, and call that from C. There's an example doing
this from Objective C in the hs-plugins paper.

-- Don

More information about the Haskell mailing list