[Haskell-cafe] FFI question

Esa Ilari Vuokko eivuokko at gmail.com
Thu Feb 9 05:36:28 EST 2006

On 2/9/06, Frederico Franzosi <ffranzosi at gmail.com> wrote:


I don't really know how this stuff exactly works, but here's few
things I have found useful when I ran into this sort of problems.

> I'm developping a package wich imports C functions.

> For example if I use:
> $ghc -package PKGname-PKGversion -fffi -o main Main.hs -llib
> it gives an error message (undefined references of objects from PKG to
> functions in lib)

I don't have exact answer, but ghc uses ld under the hood and sometimes
the order of libraries on ld command line matters.  Here's few things that
might be useful if that's the case.

Use ghc -v and find final link ld/gcc (I don't recall which one ghc invokes
for linking) and try that line yourself, you can also move libraries up on
list etc to find what order you actually need.

Add -llib to the options in package.conf as a library- try adding as first
and try adding as last.  Try both ;-)  Try adding as object file, ie

Order of arguments  on ghc command line doesn't matter so much, because
ghc will put (if I recall correctly) package libraries first and then
add -llib and
such options to the end on the ld command line- so it's pretty hard to work
around the problem just by invoking ghc different way.

> $ghc -fffi -o main Main.hs -llib /path/to/PKGlib.a
> it compiles correctly.
> and also when I do:
> $ghc -package PKGname-PKGversion -fffi -o main Main.hs /path/to/lib.a

If I were to guess these work because /path/to/lib.a is handled as an
object file, which is somehow different from being library.  I have no knowledge
what and why there is this sort of magic restrictions.


More information about the Haskell-Cafe mailing list