微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

模块“main:Main”定义在多个文件中——但它们是同一个文件

如何解决模块“main:Main”定义在多个文件中——但它们是同一个文件

我使用 stack new demo 启动了一个新的堆栈项目,并在 apps 文件夹中定义了第二个文件 Other.hs。没有依赖关系,只是:

module Other where

main :: IO ()
main = print 4

并在package.yaml下的executables添加

  other-exe:
    main:                Other.hs
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - debug-multiple-files

现在当我执行 stack build 时,我得到:

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

所以我将 -main-is 添加到 ghc-options:

    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    - -main-is Other

现在 stack build 有效,但在 stack-ghci 上,在选择了我得到的其中一个可执行文件后:

<no location info>: error:
    module ‘main:Main’ is defined in multiple files: /home/.../demo/app/Main.hs
                                                     /home/.../demo/app/Main.hs
Failed,no modules loaded.

解决方法

正如这里所指出的:https://stackoverflow.com/a/61393123other-modules: [] 添加到每个可执行文件块会有所帮助。 所以最后一个块是:

  other-exe:
    main:                Other.hs
    other-modules:       []
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    - -main-is Other
    dependencies:
    - demo

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。