Search code examples
windowshaskellpackagingcabalpkg-config

How to specify dependency on external C library in .cabal?


I maintain a library with FFI bindings on Hackage. So my Haskell library depends on the corresponding C library and its header files. Now I specify the external dependency in the .cabal file like this:

PkgConfig-Depends:
      libfoo >= 1.2

And it works well for me in Linux. However, I have a user of the library who reports, that installing pkg-config on Windows is rather cumbersome, and instead he prefers

Includes:
      foo.h
Extra-libraries:
      foo

I'd like my library to be as easy to build as possible, and don't want to force build dependencies which are not strictly required. However, I see that Cabal manual suggests to use PkgConfig-Depends.

My questions:

  • Which way I should prefer for cross-platform packages?
  • Is it possible to write a .cabal file in such a way, that it can work with pkg-config and without?
  • And, by the way, is pkg-config included in the Haskell platform (I don't have a Windows machine to check right now)?

Solution

  • The pkg-config method is preferable because pkg-config knows where to find include and library files, which may be in nonstandard locations on some systems.

    You can write the .cabal file to use both methods. Using a flag, as shown here, has the advantage that Cabal will automatically try the other flag value if the default fails. (Below example is not tested)

    Flag UsePkgConfig
      Description: Use pkg-config to check for library dependences
      Default: True
    
    Executable hax
      if flag(UsePkgConfig)
        PkgConfig-Depends: libfoo >= 1.2
      else
        Includes: foo.h
        Extra-libraries: foo