[Haskell-cafe] cabal: problem building ffi shared library and significance of __stginit

Anthony LODI anthony.lodi at gmail.com
Tue May 18 17:31:04 EDT 2010


Hello,

I'm trying to build some haskell code as a .so/.dll so that it can
ultimately be used by msvc.  I have it working when I compile by hand
(listed below) but I can't get the exact same thing built/linked with
cabal.  On linux everything builds fine, but when I try to link the
resulting .so file, I get an error about a missing
'__stginit_CInterface' reference.  Indeed I couldn't find that name in
any of the cabal-generated .dyn_o files.  I checked the output of
'cabal build -v' and it seems to be executing about the same thing
that I'm executing manually so I'm not sure what could be going wrong.
 On windows cabal won't even configure since '--enable-shared' seems
to imply '-dynamic' (right?), and that's not currently supported.

Also, when I remove the line 'hs_add_root(__stginit_CInterface);', and
the corresponding forward declaration, the program runs fine!  Does
ghc no longer need this call or are my toy programs just being lucky
sofar?

Cheers,

- Anthony LODI


=========================================

(archlinux32 -- 2.6.32)

> hsc2hs CInterface.hsc

> ghc --make CInterface

> cat dll_init.h

#pragma once

extern void __stginit_CInterface(void);

extern void sp_init(int*, char***);
extern void sp_exit(void);

> cat dll_init.c

#include "HsFFI.h"
#include "dll_init.h"

void sp_init(int* p_argc, char*** p_argv) {
  hs_init(p_argc, p_argv);
  hs_add_root(__stginit_CInterface);
}

void sp_exit(void) {
  hs_exit();
}

> ghc -c dll_init.c

> ghc -o libfoo.so -shared -dynamic CInterface.o CInterface_stub.o dll_init.o

> ghc -c ctest.c

> ghc -threaded -o ctest -L. -lfoo ctest.o -optl-Wl,-rpath,'$ORIGIN'

> ./ctest

=========================================

(winxp is similar except without '-threaded' or '-dynamic')

=========================================

> cat test.cabal

...

library
  build-depends: base >=4 && <5
  exposed-modules: CInterface
  c-source: dll_init.c
  ghc_options: -threaded

> cabal clean
> cabal configure --enable-shared
> cabal build


More information about the Haskell-Cafe mailing list