如何让栈传递自定义标志给 GHC(以及 CPP 预处理器)?

huangapple go评论56阅读模式
英文:

How do you get stack to pass custom flags to ghc (and the cpp preprocessor)?

问题

以下是您提供的代码的翻译部分:

我在我的文件中有一些条件编译,比如

```haskell
#if FLAG
bogus1 x = x + 1
#else
bogus2 x = x + 2
#endif

然后我尝试将package.yaml文件设置为在构建时将标志传递给ghc。因此,我将以下内容添加到文件中:

executables:
  my_maim:
    main:                Main.hs
    source-dirs:         app
    ghc-options:
    - "-DFLAG"
    - -main-is Main
    dependencies:
    - MyPackage

这不起作用(我仍然看到bogus2),但它给了我一个有用的警告:“警告:请改用cpp-options: -DFGLAG,而不是ghc-options: -DFGLAG”。好吧,于是我尝试了:

executables:
  my_maim:
    main:                Main.hs
    source-dirs:         app
    cpp-options:
    - "-DFLAG"
    ghc-options:
    - -main-is Main
    dependencies:
    - MyPackage

然后使用stack build构建不会传递正确的标志(我仍然看到bogus2)。

另一方面,我可以执行stack build -ghc-options --DFLAG,这样可以正常工作!(最终我看到bogus1)。

我做错了什么,如何设置Stack以传递正确的标志?


请注意,翻译是基于您提供的内容进行的,不包括任何附加信息。如果您需要进一步的帮助或有其他问题,请随时提出。

<details>
<summary>英文:</summary>

I have some conditional compilation in my files, say

#if FLAG
bogus1 x = x + 1
#else
bogus2 x = x + 2
#endif


Then I&#39;m trying to set my `package.yaml` to pass the flag to `ghc` on build. So I added the following to the file:

executables:
my_maim:
main: Main.hs
source-dirs: app
ghc-options:
- "-DFLAG"
- -main-is Main
dependencies:
- MyPackage


This does not work (I still see `bogus2`) but it gives me a useful warning `Warning: Instead of &#39;ghc-options: -DFGLAG&#39; use &#39;cpp-options: -DFGLAG&#39;`. Fair enough, so I tried:

executables:
my_maim:
main: Main.hs
source-dirs: app
cpp-options:
- "-DFLAG"
ghc-options:
- -main-is Main
dependencies:
- MyPackage


Then building with `stack build` does not pass the right flags (I still see `bogus2`). 

On the other hand, I can do `stack build -ghc-options --DFLAG`, which works! (I finally see `bogus1`). 

What am I doing wrong, and how do you set stack to pass the right flags?

</details>


# 答案1
**得分**: 1

我猜你可能在除了 Main.hs 之外的文件中使用了该标志。executables: 下的选项仅影响 app/Main.hs。你可以将你的 cpp-options: 配置放在顶层或者在 library: 下。


顶层 (缩进为0)

cpp-options:

  • -DFLAG

library:

... (其他字段)

cpp-options:

  • -DFLAG

<details>
<summary>英文:</summary>

I&#39;m guessing that you are using that flag in files other than `Main.hs`. The options under `executables:` only affect `app/Main.hs`. You can put your `cpp-options:` config at the toplevel or under `library:` instead.

Toplevel (indentation 0)

cpp-options:

  • -DFLAG

library:

... (other fields)

cpp-options:

  • -DFLAG

</details>



huangapple
  • 本文由 发表于 2023年3月4日 06:45:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632468.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定