[Haskell-cafe] FFI, TH or GHCI, foreign bindings and the linker(s)

Andrea Rossato mailing_list at istitutocolli.org
Sun Dec 7 13:08:28 EST 2008


I know that this is somehow a recurring question, still the archives
have not been helpful for finding a working solution.

I'm writing the bindings[1] to bibutils[2], a set of utilities for
converting from and to different bibliographic databases (MODS,
bibtex, etc.).

bibutilis uses a static library (which is not installed) to build a
set of different binaries which are the installed utilities.

Suppose I build the library in /tmp/bibutils.

If I build the haskell bindings (hs-bibutils) with:

runhaskell Setup.lhs configure --extra-include-dirs=/tmp/bibutils/lib --extra-lib-dirs=/tmp/bibutils/lib

where libbibutils.a and the header files are located, then I can build
a simple test[3] program with ghc --make test.hs and it works

When I tried to link citeproc-hs to hs-bibutils everything worked
fine, too. But when I tried building pandoc, which uses the Template
Haskell extension to generate some code at compilation time, I got a
linker error when TH started its process:


    [ 6 of 29] Compiling Text.Pandoc.ODT  ( Text/Pandoc/ODT.hs, dist/build/Text/Pandoc/ODT.o )
    Loading package ghc-prim ... linking ... done.


    Loading package template-haskell ... linking ... done.
    ghc: /tmp/bibutils_3.43/lib/bibutils.o: unknown symbol `fields_add'
    Loading package hs-bibutils-0.1 ... linking ... ghc: unable to load package `hs-bibutils-0.1'

So I went back to the test file with the original bindings. If I try
to load it and run it on ghci I get the very same error:

    Prelude Main> main
    Loading package syb ... linking ... done.
    Loading package hs-bibutils-0.1 ... linking ... <interactive>: /tmp/bibutils/lib/bibutils.o: unknown symbol `fields_add'
    ghc: unable to load package `hs-bibutils-0.1'

After searching the we I found this:

which states that GHCi cannot load static libraries (the same can be
argued by reading the GHCi docs, indeed).

After reading this thread:

I came to know that GHCi and GHC are not using the same linker, and,
as far as I understand, TH uses the first one too.

So I tried to build a dynamic library.

ar -t /tmp/bibutils/lib/libbibutils.a

to get the list of objects to link with:

gcc -shared -o libbibutils.so *.o

after compiling them with the -fPIC flag.

But this library is not being loaded because of some undefined
lib/libbibutils.so: undefined reference to `corps'
lib/libbibutils.so: undefined reference to `asis'
lib/libbibutils.so: undefined reference to `progname'

These symbols refer to some variables defined as external entities,
for instance: extern char progname[]; these entities are initialized
by a stub.c file in the Haskell bindings:

Unfortunately my knowledge of such low level stuff is very very
limited and I'm not seeing any way out. Is there one?


[1] http://code.haskell.org/~arossato/hs-bibutils/
[2] http://www.scripps.edu/~cdputnam/software/bibutils/
[3] the test.hs file
import Text.Bibutils

main :: IO ()
main = do
  init_globals "mods2bibtex"
  bibl  <- bibl_init
  param <- bibl_initparams mods_in bibtex_out
  setFormatOpts param [bibout_brackets, bibout_uppercase]
  setBOM   param
  setVerbose param
  bibl_read  param bibl "/tmp/prova.biblio" mods_in
  bibl_write param bibl "-"                    bibtex_out
  return ()

More information about the Haskell-Cafe mailing list