在默认的Haskell Stack项目中生成多个可执行文件 [英] Building multiple executables in the default Haskell Stack project
问题描述
我使用默认的 stack new
设置了一个项目,该项目具有作为单独的可执行文件的服务器和客户端.我以正确的方式更改了 package.yaml
文件(截至2020年4月21日," app 目录添加了一个名为 Client.hs
的新文件
I used the default stack new
to setup a project that has a server and a client as separate executables. I altered the package.yaml
file in what seems like the right way (As of April 21, 2020 "There is no user guide") and added a new file to my app
directory called Client.hs
.
我收到一条错误消息:非法启用其他模块"中列出的主模块"Main"的解决方法!"
I got an error saying "Enabling workaround for Main module 'Main' listed in 'other-modules' illegally!"
我如何通过堆栈构建客户端和服务器?
How do I have stack build both the client and the server?
当我运行 stack build
时,我得到了:
When I ran 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 的相关部分如下所示:
The relevant portion of package.yaml
looks like this:
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
推荐答案
这里有两个问题.首先, 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.
这篇关于在默认的Haskell Stack项目中生成多个可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!