Compile and link a Haskell package against a local C library

Published on

Let’s say you want to build a Haskell package with a locally built version of a C library for testing/debugging purposes. Doing this is easy once you know the right option names, but finding this information took me some time, so I’m recording it here for the reference.

Let’s say the headers of your local library are in /home/user/src/mylib/include and the library files (*.so or *.a) are in /home/user/src/mylib/lib. Then you can put the following into your stack.yaml (tested with stack v2.2.0; instructions for cabal-install should be similar):

extra-include-dirs:
  - /home/user/src/mylib/include
extra-lib-dirs:
  - /home/user/src/mylib/lib
ghc-options:
  "$locals": -optl=-Wl,-rpath,/home/user/src/mylib/lib

Here "$locals" means “apply the options to all local packages”.