在需要LANGUAGE CPP的模块上运行ghci

Eri*_*ikR 7 c++ haskell ghc ghci cabal

我遇到了这一系列命令的问题:

wget http://hackage.haskell.org/package/github-0.7.1/github-0.7.1.tar.gz
tar zxf github-0.7.1.tar.gz
cd github-0.7.1
ghci samples/Users/ShowUser.hs
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

Github/Private.hs:142:0:
     error: missing binary operator before token "("

Github/Private.hs:148:0:
     error: missing binary operator before token "("
phase `C pre-processor' failed (exitcode = 1)
Run Code Online (Sandbox Code Playgroud)

那是因为Github/Private.hs模块使用的 cpp在两个地方指令:

#if MIN_VERSION_http_conduit(1, 9, 0)
    successOrMissing s@(Status sci _) hs cookiejar
#else
    successOrMissing s@(Status sci _) hs
#endif
      | (200 <= sci && sci < 300) || sci == 404 = Nothing
#if MIN_VERSION_http_conduit(1, 9, 0)
      | otherwise = Just $ E.toException $ StatusCodeException s hs cookiejar
#else
      | otherwise = Just $ E.toException $ StatusCodeException s hs
#endif
Run Code Online (Sandbox Code Playgroud)

这似乎ghci是对这些CPP指令的窒息.但是,cabal install成功编译并安装包.使用ghci -XCPP没有帮助.

我的问题是:如何使用此包目录中的库代码运行示例程序(即samples目录中的一个)?ghciGithub

我想尝试调整示例程序和库代码,所以我想要运行所有内容ghci.

有效的一件事是:

cabal install
cd samples
ghci Users/ShowUser.hs
Run Code Online (Sandbox Code Playgroud)

但是,再次,我宁愿不必安装库代码只是为了测试它.

ben*_*ofs 11

以下命令有效:

ghci -optP-include -optPdist/build/autogen/cabal_macros.h samples/Users/ShowUser.hs
Run Code Online (Sandbox Code Playgroud)

它告诉C预处理器读取文件dist/build/autogen/cabal_macros.h.此文件由生成cabal build,但您可以在preprocessing步骤后中止它:

Resolving dependencies...
Configuring github-0.7.1...
Building github-0.7.1...
Preprocessing library github-0.7.1...
[ 1 of 24] Compiling Github.Data.Definitions ( Github/Data/Definitions.hs,    dist/build/Github/Data/Definitions.o )
^C
Run Code Online (Sandbox Code Playgroud)

如果您希望在该目录中启动ghci时自动设置这些选项,请.ghci使用以下内容创建一个文件:

:set -optP-include -optPdist/build/autogen/cabal_macros.h
Run Code Online (Sandbox Code Playgroud)


dup*_*ode 6

问题不在于C预处理器本身,但是这些MIN_VERSION_*宏是在构建时由cabal生成的,因此您不会在GHCi中获取它们.如果您只想在不安装库的情况下使用库,那么阻力最小的路径就是注释掉宏,以及CPP条件的分支与http-conduit您当前拥有的版本不匹配(如果有疑问,请检查与ghc-pkg list).


稍微有点原则性的黑客将使用CPP检查您是否使用cabal进行安装.假设http_conduit >= 1.9.0它可能看起来像这样:

#ifdef CABAL
#  if MIN_VERSION_http_conduit(1, 9, 0)
    successOrMissing s@(Status sci _) hs cookiejar
#  else
    successOrMissing s@(Status sci _) hs
#  endif
      | (200 <= sci && sci < 300) || sci == 404 = Nothing
#  if MIN_VERSION_http_conduit(1, 9, 0)
      | otherwise = Just $ E.toException $ StatusCodeException s hs cookiejar
#  else
      | otherwise = Just $ E.toException $ StatusCodeException s hs
#  endif
#else
    successOrMissing s@(Status sci _) hs cookiejar
      | (200 <= sci && sci < 300) || sci == 404 = Nothing
      | otherwise = Just $ E.toException $ StatusCodeException s hs cookiejar
#endif
Run Code Online (Sandbox Code Playgroud)

但是考虑到你的用例,我不认为额外的步骤是值得的.


为了完整起见:这个答案解释了如何在GHCi中使用cabal宏.但是,这样做cabal build至少需要运行一次.