haskell - GHC 在 cabal 沙箱中找不到模块

标签 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 沙箱初始化

$ 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.

启用详细标志后,似乎我的 cabal 被后来的 cabal 所笼罩,而后者又反过来笼罩了早期的 cabal 。为什么会发生这种情况?

$ 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: 

最佳答案

如果您喜欢手动黑客攻击,您可以将沙箱的 pkg 数据库位置传递给 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。

编辑 cabal 文件时,请检查导出的模块(如果是库)是否已列出,并且主 src 是否正确。还要确保在 build-depends: 子句中列出诸如 natural-numbers 之类的依赖项。

关于haskell - GHC 在 cabal 沙箱中找不到模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34128474/

相关文章:

haskell - 如何在 Haskell 中创建两个具有内部状态的相互生产者/消费者?

haskell - 没有因使用 ‘+’ Haskell 而产生的 (Num a) 实例

haskell - 我可以告诉 GHC 任意选择使用哪个实例,因为我不在乎吗?

haskell - 为什么 Haskell 中的这种类型注释是必要的?

haskell - 我怎么能提前知道 cabal 将生成的可执行文件放在哪里?

haskell - 是否可以组合类型类的实例?

haskell - 除 * 之外的 child 的字符串表示形式

haskell - 两次函数调用但只显示一条跟踪

haskell - 如何使用 cabal install 安装黑线鳕

haskell - 如何在 cabal 沙箱中运行脚手架 Yesod 网站?