我尝试在以下几个方面使用ifdef:
{-# LANGUAGE CPP #-}
main :: IO ()
main = do
print "hello"
#ifdef APP_DEBUG
print "test"
#endif然而,其结果是:
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文件的适当部分将被跳过.
发布于 2021-11-29 16:43:19
指令写在行的开头,因此:
{-# LANGUAGE CPP #-}
main :: IO ()
main = do
print "hello"
#ifdef APP_DEBUG
print "test"
#endifC预处理器在Haskell编译器之前运行。因此,它不理解Haskell,例如,它将删除某个代码片段(在本例中,如果没有定义APP_DEBUG )。
作为@chi says,将哈希(#)写入第一行就足够了,因此:
{-# LANGUAGE CPP #-}
main :: IO ()
main = do
print "hello"
# ifdef APP_DEBUG
print "test"
# endif也是可能的。
编译时,可以将APP_DEBUG标志设置如下:
ghc -DAPP_DEBUG x.hshttps://stackoverflow.com/questions/70158162
复制相似问题