Problems with FFI linking?

I was trying to write a binding to a foreign library (of which the dev version is installed by apt in my system, liberfa ). stack build errors out on cabal:

Cabal-simple_mPHDZzAJ_2.4.0.1_ghc-8.6.5: Missing dependencies on foreign
libraries:
* Missing (or bad) C libraries: liberfa, liberfa
[ ... and then a block of text about either using -L or installing it globally with the system package manager ...]

It is installed but even if I pass the location with -L explicitly I still get the same error:

scasc-@local:~/dev/test-binding$ find /usr -name 'liberfa*'
/usr/lib/x86_64-linux-gnu/liberfa.a
/usr/lib/x86_64-linux-gnu/liberfa.so
/usr/share/doc/liberfa-dev
scasc-@local:~/dev/test-binding$ stack build --ghc-options -L/usr/lib/x86_64-linux-gnu
Building all executables for `haskell-bench' once. After a successful build of all of them, only specified executables will be rebuilt.
configure (lib + exe)
Configuring haskell-bench-0.1.0.0...
Cabal-simple_mPHDZzAJ_2.4.0.1_ghc-8.6.5: Missing dependencies on foreign
libraries:
* Missing (or bad) C libraries: liberfa, liberfa

I even tried copying the .a and .so files into the development libraries and adding the location to extra-lib-dirs . Any ideas how I could proceed?

1 Like

Welcome to the Haskell discourse!

From this page, it seems that liberfa includes a pkgconfig file named erfa. Does pkgconf --libs erfa and/or pkgconf --cflags erfa print sensible information?

If that works, it should just be a matter of adding the line extra-libraries: erfa to your cabal file. No need to manually put in -L or include-dirs.

If that doesn’t work, it would be useful to see your cabal file or repository so we can reproduce the error.

Hope this helps!

One of my problems was using liberfa instead of erfa – a wrong assumption on my side.

The solution I was ultimately guided to on /r/haskell was using

pkg-config-dependencies:
- erfa

instead of extra-libraries:.

However the FFI call takes about a decimal order of magnitude more time than I was expecting it to (based on info I found in the Interwebz). Maybe my binding included some unnecessary marshalling? I don’t know.

My conclusion, however, is, that for these relatively simple functions it’s in fact easier to port the code than to write bindings.

The only pros of the bindings is certainly aligning to the reference implementation, ie. no accidental deviations introduced by the porting.

1 Like