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

♀尐吖头ヾ 提交于 2019-11-30 06:46:55

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

pkg-config is not included in the Haskell Platform, nor could I imagine that it ever would be.

Usually I will use includes/Extra-libraries if they're relatively simple. But for complex packages that may have a lot of included libraries, such as gtk, it's much nicer to use pkg-config when available.

It is possible to write a .cabal file that will work with and without specific fields. Try this:

if os(windows)
  Includes:
      foo.h
  Extra-libraries:
      foo
else
  PkgConfig-Depends:
      libfoo >= 1.2

Also note that .cabal can run a configure script, which can help in some situations but isn't very windows-friendly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!