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

2024-02-25

我用的是默认的stack new设置一个将服务器和客户端作为单独的可执行文件的项目。我改变了package.yaml以看似正确的方式提交文件(截至 2020 年 4 月 21 日”没有用户指南 https://github.com/sol/hpack#there-is-no-user-guide”)并添加了一个新文件到我的app名为的目录Client.hs.

我收到一条错误消息“为‘其他模块’中列出的主模块‘Main’非法启用解决方法!”

如何同时构建客户端和服务器的堆栈?

当我跑的时候stack build I got:

[... clip ...]
Building executable 'ObjectServer' for ObjectServer-0.1.0.1..
[4 of 4] Compiling Client
Linking .stack-work\dist\29cc6475\build\ObjectServer\ObjectServer.exe ...
Warning: Enabling workaround for Main module 'Main' listed in 'other-modules'
illegally!
Preprocessing executable 'Client' for ObjectServer-0.1.0.1..
Building executable 'Client' for ObjectServer-0.1.0.1..
[3 of 3] Compiling Client

<no location info>: error:
    output was redirected with -o, but no output will be generated
because there is no Main module.


--  While building package ObjectServer-0.1.0.1 using:
      D:\HaskellStack\setup-exe-cache\x86_64-windows\Cabal-simple_Z6RU0evB_3.0.1.0_ghc-8.8.3.exe --builddir=.stack-work\dist\29cc6475 build lib:ObjectServer exe:Client exe:ObjectServer --ghc-options " -fdiagnostics-color=always"
    Process exited with code: ExitFailure 1

的相关部分package.yaml看起来像这样:

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

这里有两个问题。首先,默认值other-modules in hpack是“所有模块source-dirs except main和模块中提到的when子句”。如果你看看生成的.cabal文件中,您会看到由于此默认设置,每个可执行文件都错误地将另一个可执行文件的模块包含在其other-modules列表。其次,main设置给出包含主模块的源文件,但不会更改 GHC 所需的模块名称Main到其他任何事情。因此,那module仍然需要命名module Main where ..., not module Client where...,除非您还单独添加-main-is ClientGHC 选项。

所以,我建议修改Client.hs使其成为Main module:

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

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

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

这在我的测试中似乎有效。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在默认的 Haskell Stack 项目中构建多个可执行文件 的相关文章

随机推荐