首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Haskell示例如何使用ifdef语言实用化?

Haskell示例如何使用ifdef语言实用化?
EN

Stack Overflow用户
提问于 2021-11-29 16:40:43
回答 1查看 159关注 0票数 2

我尝试在以下几个方面使用ifdef

代码语言:javascript
复制
{-# LANGUAGE CPP #-}

main :: IO ()
main = do
  print "hello"
  #ifdef APP_DEBUG
    print "test"
  #endif

然而,其结果是:

代码语言:javascript
复制
x.hs:6:3: error: Variable not in scope: (#) :: IO () -> t1 -> t0
  |
6 |   #ifdef APP_DEBUG
  |   ^

x.hs:6:4: error:
    Variable not in scope: ifdef :: t2 -> (a0 -> IO ()) -> [Char] -> t1
  |
6 |   #ifdef APP_DEBUG
  |    ^^^^^

x.hs:6:10: error: Data constructor not in scope: APP_DEBUG
  |
6 |   #ifdef APP_DEBUG
  |          ^^^^^^^^^
...

我哪里出问题了?

在ghc的文档中,我似乎找不到足够的信息:https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/utils.html?highlight=ifdef

我发现的只是:

#if⟨⟩、#ifdef⟨name⟩、#ifndef⟨name⟩、#elif⟨condition⟩、#else、#endif、#error⟨message⟩、#warning message⟩条件编译指令不受修改地传递给C程序、C文件和C头。将它们放入C程序意味着Haskell文件的适当部分将被跳过.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-29 16:43:19

指令写在行的开头,因此:

代码语言:javascript
复制
{-# LANGUAGE CPP #-}

main :: IO ()
main = do
  print "hello"
#ifdef APP_DEBUG
  print "test"
#endif

C预处理器在Haskell编译器之前运行。因此,它不理解Haskell,例如,它将删除某个代码片段(在本例中,如果没有定义APP_DEBUG )。

作为@chi says,将哈希(#)写入第一行就足够了,因此:

代码语言:javascript
复制
{-# LANGUAGE CPP #-}

main :: IO ()
main = do
    print "hello"
#   ifdef APP_DEBUG
    print "test"
#   endif

也是可能的。

编译时,可以将APP_DEBUG标志设置如下:

代码语言:javascript
复制
ghc -DAPP_DEBUG x.hs
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70158162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档