英文:
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'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 'ghc-options: -DFGLAG' use 'cpp-options: -DFGLAG'`. 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'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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论