且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在默认的Haskell Stack项目中生成多个可执行文件

更新时间:2023-09-30 11:58:46

这里有两个问题.首先, hpack other-modules 的默认值是" source-dirs 中的所有模块,除了 main when 子句中提到的模块".如果查看生成的 .cabal 文件,则会发现由于此默认设置,每个可执行文件在其 other-modules 中都错误地将另一个可执行文件的模块包含在内.列表.其次, main 设置提供了包含主模块的源文件,但不会将GHC期望的模块名称从 Main 更改为其他任何文件.因此,该 module 仍需要命名为 module Main where ... ,而不是 module Client where ... ,除非您也分别命名为添加 -main-is Client GHC选项.

There are two problems here. First, the default value for other-modules in hpack is "all modules in source-dirs except main and modules mentioned in a when clause". If you look at the generated .cabal file, you'll see that as a result of this default, each executable has incorrectly included the other executable's module in its other-modules list. Second, the main setting gives the source file that contains the main module, but doesn't change the name of the module expected by GHC from Main to anything else. Therefore, that module still needs to be named module Main where ..., not module Client where..., unless you also, separately add a -main-is Client GHC option.

因此,我建议修改 Client.hs 以使其成为 Main 模块:

So, I would advise modifying Client.hs to make it the Main module:

-- in Client.hs
module Main where
...

,然后为两个可执行文件明确指定 other-modules:[] :

and then specifying other-modules: [] explicitly for both executables:

executables:
  ObjectServer:
    main:                Main.hs
    other-modules:       []
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - ObjectServer
  Client:
    main:                Client.hs
    other-modules:       []
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - ObjectServer

这似乎在我的测试中起作用.

That seems to work in my testing.