简体   繁体   English

如何在.cabal中指定对外部C库的依赖?

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

I maintain a library with FFI bindings on Hackage. 我在Hackage上维护了一个带有FFI绑定的库。 So my Haskell library depends on the corresponding C library and its header files. 所以我的Haskell库依赖于相应的C库及其头文件。 Now I specify the external dependency in the .cabal file like this: 现在我在.cabal文件中指定外部依赖项,如下所示:

PkgConfig-Depends:
      libfoo >= 1.2

And it works well for me in Linux. 它在Linux中对我很有用。 However, I have a user of the library who reports, that installing pkg-config on Windows is rather cumbersome, and instead he prefers 但是,我有一个库的用户报告,在Windows上安装pkg-config相当麻烦,而他更喜欢

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 . 但是,我看到Cabal手册建议使用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? 是否有可能以这样的方式编写.cabal文件,它可以与pkg-config而没有?
  • And, by the way, is pkg-config included in the Haskell platform (I don't have a Windows machine to check right now)? 顺便说一下,Haskell平台中是否包含pkg-config (我现在没有Windows机器可以检查)?

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. pkg-config方法更可取,因为pkg-config知道在哪里可以找到包含文件和库文件,这些文件可能位于某些系统的非标准位置。

You can write the .cabal file to use both methods. 您可以编写.cabal文件以使用这两种方法。 Using a flag, as shown here, has the advantage that Cabal will automatically try the other flag value if the default fails. 使用标志,如此处所示,具有以下优点:如果默认值失败,Cabal将自动尝试其他标志值。 (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. pkg-config不包含在Haskell平台中,我也无法想象它会是什么。

Usually I will use includes/Extra-libraries if they're relatively simple. 通常我会使用includes/Extra-libraries如果它们相对简单的话。 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. 但对于可能包含很多库的复杂软件包,例如gtk,在可用时使用pkg-config要好得多。

It is possible to write a .cabal file that will work with and without specific fields. 可以编写一个可以使用和不使用特定字段的.cabal文件。 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. 另请注意, .cabal可以运行配置脚本,这在某些情况下可以提供帮助,但对Windows不友好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM