【问题标题】:How can I configure Cabal to build two executables?如何配置 Cabal 以构建两个可执行文件?
【发布时间】:2020-04-02 01:17:05
【问题描述】:

我正在尝试使用 Cabal 构建 a Haskell project。我有一个文件 src/Main.hs,其中包含模块 Main 和函数 main。该文件运行我的应用程序的 Web 界面。我还有另一个文件 src/CLI.hs,其中包含模块 CLI 和函数 main。我可以使用runhaskell CLI ... 很好地运行它,但我似乎无法使用 cabal 编译它。

问题是,即使我将 CLI.hs 指定为主文件 (main-is: CLI.hs),它仍然会使用来自 src/Main.hs 的 main 编译项目,从而为我提供 Web 应用程序而不是命令行界面。

我希望能够编译两个可执行文件,一个是 Web 应用程序,一个是 CLI,在 CLI.hs 中将每个的入口点指定为 main,在 Main.hs 中指定为 main,分别。

这是我目前正在使用的 .cabal 文件的片段:

executable color-word-analyzer-cli
  main-is:           CLI.hs
  other-modules:     AnnotateColors
                   , CategorizeColor
                   , ColorMaps
                   , FindColors
                   , PlotColors
                   , Types
                   , Main
  build-depends:       base
                     , lucid
                     ...
                     , wai-middleware-static
  hs-source-dirs:      src/
  default-language:    Haskell2010

executable color-word-analyzer-web
  main-is:           Main.hs
  other-modules:     AnnotateColors
                   , CategorizeColor
                   , ColorMaps
                   , FindColors
                   , PlotColors
                   , Types
                   , CLI
  build-depends:     base
                   , lucid
                     ...
                   , wai-middleware-static
  hs-source-dirs:      src/
  default-language:    Haskell2010

哪个会引发错误(以及其他):

Building executable 'color-word-analyzer-cli' for color-word-analyzer-0.1.0.0..
Warning: Enabling workaround for Main module 'Main' listed in 'other-modules'
illegally!

<no location info>: warning: [-Wmissing-home-modules]
    These modules are needed for compilation but not listed in your .cabal file's other-modules: 
        Main

这很有趣,因为Main 清楚地列在other-modules 中。

我使用的是 cabal 版本 3.0.0.0 和 ghc 版本 8.8.2。

【问题讨论】:

    标签: haskell cabal


    【解决方案1】:

    编译单元的完全显式形式是&lt;package&gt;:&lt;category&gt;:&lt;ident&gt;,如果包是color-word-analyzer,类别是exe,ident 是可执行文件。因此,对于您的情况,您可以致电:

    cabal build color-word-analyzer:exe:color-word-analyzer-cli color-word-analyzer:exe:color-word-analyzer-web
    

    现在您实际上不需要指定所有这些。当可执行文件是唯一的时,它是一个可执行文件的事实(而不是来自其他包或另一个包名称本身的测试)以及它来自 color-word-analyzer 的事实在这种情况下是很清楚的。因此,您可以调用:

     cabal build color-word-analyzer-cli color-word-analyzer-web
    

    编辑:因为您的链接没有-web 的节,所以我使用了我自己的一个不包括CLI 模块的创作。请注意,您的 CLI 文件是 module Main,这解释了您看到的错误 - 您不能将模块 Main 包含为库模块。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-02
      • 1970-01-01
      • 2014-04-21
      相关资源
      最近更新 更多