GHC在cabal沙箱中找不到模块

时间:2015-12-07 07:32:20

标签: haskell ghc cabal cabal-install

我在(X)Ubuntu 15.10上使用Haskell版本7.8.4,而Cabal-Install 1.18都通过apt安装。我没有尝试在这台机器上手动安装任何与Haskell相关的东西。我设置了一个cabal沙箱,只提取并安装了一个模块,发现ghc似乎没有拿起它。 ghc -v似乎暗示我有两个版本的cabal库相互遮蔽。这是如何工作的?

我开始使用空目录/tmp/haskell-example

然后我做cabal sandbox init

$ cabal sandbox init
Writing a default package environment file to
/tmp/haskell-example/cabal.sandbox.config
Creating a new sandbox at /tmp/haskell-example/.cabal-sandbox

然后我安装natural-numbers,因为我想在程序中使用Data.Natural模块。此操作成功。

$ cabal install natural-numbers
Resolving dependencies...
Notice: installing into a sandbox located at
/tmp/haskell-example/.cabal-sandbox
Configuring natural-numbers-0.1.2.0...
Building natural-numbers-0.1.2.0...
Installed natural-numbers-0.1.2.0

我可以验证Data.Natural模块确实已安装到cabal沙箱中。

$ ls /tmp/haskell-example/.cabal-sandbox/lib/x86_64-linux-ghc-7.8.4/natural-numbers-0.1.2.0
 Data
libHSnatural-numbers-0.1.2.0.a
libHSnatural-numbers-0.1.2.0-ghc7.8.4.so
$ ls /tmp/haskell-example/.cabal-sandbox/lib/x86_64-linux-ghc-7.8.4/natural-numbers-0.1.2.0/Data
Natural.dyn_hi
Natural.hi

然后我创建一个简单的Main.hs文件,导入Data.Natural但不使用它。

module Main where

import Data.Natural

main = putStrLn "Hello World"

当我尝试ghc Main.hs时,我会看到以下内容:

$ ghc Main.hs 
Main.hs:3:8:
    Could not find module ‘Data.Natural’
    Use -v to see a list of the files searched for.

启用详细标志后,似乎我的阴谋被后来的阴影所遮蔽了,后者又影响了早期的阴谋集团。为什么会这样?

$ ghc -v Main.hs 
Glasgow Haskell Compiler, Version 7.8.4, stage 2 booted by GHC version 7.8.4
Using binary package database: /usr/lib/ghc/package.conf.d/package.cache
hiding package Cabal-1.18.1.5 to avoid conflict with later version Cabal-1.22.1.1
wired-in package ghc-prim mapped to ghc-prim-0.3.1.0-ec14d2f6075975a2ce9112eae431c8e1
wired-in package integer-gmp mapped to integer-gmp-0.5.1.0-de4898ebdc5ab81cedce89121ae9ac84
wired-in package base mapped to base-4.7.0.2-5ef1e7e809bc3b18d74efc783356e209
wired-in package rts mapped to builtin_rts
wired-in package template-haskell mapped to template-haskell-2.9.0.0-c1976a420ad8b9b589eee08844c59ba2
wired-in package dph-seq not found.
wired-in package dph-par not found.
Hsc static flags: 
hiding package Cabal-1.18.1.5 to avoid conflict with later version Cabal-1.22.1.1
wired-in package ghc-prim mapped to ghc-prim-0.3.1.0-ec14d2f6075975a2ce9112eae431c8e1
wired-in package integer-gmp mapped to integer-gmp-0.5.1.0-de4898ebdc5ab81cedce89121ae9ac84
wired-in package base mapped to base-4.7.0.2-5ef1e7e809bc3b18d74efc783356e209
wired-in package rts mapped to builtin_rts
wired-in package template-haskell mapped to template-haskell-2.9.0.0-c1976a420ad8b9b589eee08844c59ba2
wired-in package dph-seq not found.
wired-in package dph-par not found.
*** Chasing dependencies:
Chasing modules from: *Main.hs

Main.hs:3:8:
    Could not find module ‘Data.Natural’
    Locations searched:
    Data/Natural.hs
    Data/Natural.lhs
*** Deleting temp files:
Deleting: 
*** Deleting temp dirs:
Deleting: 

1 个答案:

答案 0 :(得分:7)

如果您正在进行手动黑客攻击,可以将沙箱的pkg db位置传递给ghc,例如:

ghc Main.hs -package-db .cabal-sandbox/x86_64-linux-ghc-7.10.2-packages.conf.d/

然而,使用沙箱的“正常”方式是始终使用cabal build(或cabal install没有参数)进行编译,而不是直接运行ghc。

  1. 运行cabal init,随意回答问题
  2. 修改生成的foo.cabal文件(foo是您的包名称。)
  3. 运行cabal buildcabal install - 这将为您运行ghc。
  4. 编辑cabal文件时,检查导出的模块(如果是lib)是否已列出,并且主src是否正确。还要确保natural-numbers子句中列出了build-depends:等依赖项。