我使用的是默认的 stack new
来设置一个有一个服务器和一个客户端的项目,作为单独的可执行文件。我修改了 package.yaml
文件的方式似乎是正确的(截至2020年4月21日"没有用户指南"),并将一个新文件添加到我的 app
目录称为 Client.hs
.
我收到一个错误信息说:"非法启用'其他模块'中列出的主模块'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
除了 main
中提到的模块 when
子句"。 如果你看一下生成的 .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
在我的测试中,这似乎是有效的。