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

Duncan Coutts duncan.coutts at googlemail.com
Wed May 19 13:01:12 EDT 2010


On Tue, 2010-05-18 at 17:31 -0400, Anthony LODI wrote:
> 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?

For reference for other people, Anthony and I worked this out today.

full example:
http://pastebin.com/aLdyFMPg

The difference between doing it manually and building a library via
Cabal is the package name.

When building directly with ghc, the default package name is "" aka the
main package. When building a ghc/Haskell package, the package name gets
set (ghc -package-name test-0.0). This package name gets encoded into
the symbol names. So we get:

   __stginit_testzm0zi0_CInterface
vs __stginit_CInterface

(testzm0zi0 is the Z-encoding of test-0.0)

What is bad here is that the __stginit stuff is even necessary. Anthony
is going to file a ghc ticket and/or complain on the ghc users list,
citing this example.

Duncan



More information about the Glasgow-haskell-users mailing list