【问题标题】:Why does stack not pass my ghc-options to the compiler?为什么堆栈不将我的 ghc 选项传递给编译器?
【发布时间】:2017-10-22 12:13:37
【问题描述】:

在 cabal 文件中,我指定了 GHC 选项 -Wall-O2

name:               Test
version:            0.1.0.0
build-type:         Simple
cabal-version:      >=1.8

executable Test
  hs-source-dirs:   src
  main-is:          Test.hs
  build-depends:    base >=4.8 && <4.10
  ghc-options:      -Wall -O2

当我编译程序Test.hs时:

data Color = Red | Green | Blue

foo :: Color -> Int
foo Red = 0
foo Green = 1
-- foo Blue is intentionally missing!!

我得到错误:

Preprocessing executable 'Test' for Test-0.1.0.0...
[1 of 1] Compiling Main             ( src/Test.hs, .stack-work/dist/x86_64-linux-nopie/Cabal-1.24.2.0/build/Test/Test-tmp/Main.o )

/home/user/Projekte/HaskellTutorials/Test/src/Test.hs:1:1: error:
    The IO action ‘main’ is not defined in module ‘Main’

--  While building package Test-0.1.0.0 using:
      /home/user/.stack/setup-exe-cache/x86_64-linux-nopie/Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.2 --build
      dir=.stack-work/dist/x86_64-linux-nopie/Cabal-1.24.2.0 build exe:Test --ghc-options " -ddump-hi -ddump-to-file"
    Process exited with code: ExitFailure 1

关于缺少主要动作的错误不是问题所在。 查看倒数第二行末尾的文字:

构建 exe:Test --ghc-options " -ddump-hi -ddump-to-file"

为什么我看不到我的 GHC 选项-Wall -O2? (我担心,我犯了一些愚蠢的小错误......)

PS:
堆栈版本为:版本 1.5.1,Git 修订版 600c1f01435a10d127938709556c1682ecfd694e(4861 次提交)x86_64 hpack-0.17.1
长期支持:8.17

【问题讨论】:

  • 您是否尝试将虚拟主文件 (main = putStrLn "It works!") 添加到您的 Test.hs 文件中,每个可执行文件必须包含这样的功能。 - 我不认为 ghc 有兴趣在手头有更多严重错误时发出警告

标签: haskell cabal haskell-stack


【解决方案1】:

选项-ddump-hi -ddump-to-file 正在由 Stack 传递给 Cabal。然后,Cabal 将它们添加到 .cabal 文件中指定的选项中,然后将它们传递给 GHC。

如果您使用以下方式运行 Stack:

stack -v --cabal-verbose

并搜索输出,您会发现您的选项实际上正在传递给 GHC。

正如@epsilonhalbe 所说,当 GHC 发现 main 丢失时,它并没有抱怨更小的问题,所以如果你添加:

main = undefined

在程序的底部,您会收到您所期望的警告。

【讨论】:

    猜你喜欢
    • 2018-06-23
    • 2023-04-08
    • 2013-12-01
    • 1970-01-01
    • 2021-10-10
    • 2012-08-14
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多