gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:49:09 25 4
gpt4 key购买 nike

我使用默认的 stack new设置一个将服务器和客户端作为单独的可执行文件的项目。我更改了 package.yaml以看似正确的方式(截至 2020 年 4 月 21 日“There is no user guide”)并在我的 app 中添加了一个新文件名为 Client.hs 的目录.

我收到一条错误消息,提示“非法启用在 'other-modules' 中列出的主模块 'Main' 的解决方法!”

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

当我运行 stack build我有:

[... 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的默认值在 hpack是“source-dirs 中的所有模块,mainwhen 子句中提到的模块除外”。如果查看生成的 .cabal文件中,您会看到由于此默认设置,每个可执行文件都错误地将另一个可执行文件的模块包含在其 other-modules 中。列表。二、main设置提供包含主模块的源文件,但不会更改 GHC 预期的模块名称 Main其他任何事情。因此,该模块仍需命名为 module Main where ... ,而不是 module Client where... ,除非您也单独添加 -main-is Client GHC 选项。

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

-- 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

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

关于haskell - 在默认的 Haskell Stack 项目中构建多个可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61377715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com