不会产生输出,因为没有Main模块

No output will be generated because there is no Main module

我写了一个简单的模块叫 Test:

module Test (main) where

main =
  putStrLn "Hello"

但是,当我尝试通过以下命令行编译它时:

ghc Test.hs -o my-program

我收到以下错误:

[1 of 1] Compiling Test             ( Test.hs, Test.o )

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

ghc 将假定 main 位于名为 Main 的模块中(如编译器所说)。

ghc 但是有一个选项 -main-is,您可以在其中指定 main 函数所在模块的名称。所以你可以编译:

ghc <b>-main-is Test</b> Test.hs -o my-program

我有同样的问题,但在我的情况下,我是通过向测试源文件添加模块名称引起的,test/Spec.hs:

module Tests where

main :: IO ()
main = hspec $ do ...

解决方案只是删除第一行:

main :: IO ()
main = hspec $ do ...

这也是您使用堆栈创建项目时的默认设置 - 例如,stack new myproject