简体   繁体   English

GHC在cabal沙箱中找不到模块

[英]GHC cannot find module in cabal sandbox

I am using Haskell version 7.8.4 on (X)Ubuntu 15.10, with Cabal-Install 1.18 both installed through apt . 我在(X)Ubuntu 15.10上使用Haskell版本7.8.4,通过apt安装Cabal-Install 1.18。 I have not attempted to install anything Haskell-related by hand on this machine. 我没有尝试在这台机器上手动安装任何与Haskell相关的东西。 I set up a cabal sandbox, fetched and installed a module only to discover that ghc doesn't seem to be picking it up. 我设置了一个cabal沙箱,取出并安装了一个模块,发现ghc似乎没有把它ghc起来。 ghc -v seems to suggest that I have two versions of the cabal library that are mutually shadowing each other. ghc -v似乎暗示我有两个版本的cabal库互相遮蔽。 How does this work? 这是如何运作的?

I am starting out with an empty directory /tmp/haskell-example 我开始使用空目录/tmp/haskell-example

then I do a cabal sandbox init . 然后我做了一个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

then I install natural-numbers because I want to use the Data.Natural module in a program. 然后我安装natural-numbers因为我想在程序中使用Data.Natural模块。 This operation succeeds. 此操作成功。

$ 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

I can verify that the Data.Natural module has indeed been installed to the cabal sandbox. 我可以验证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

then I create a simple Main.hs file that imports Data.Natural but does not use it. 然后我创建一个简单的Main.hs文件,导入Data.Natural但不使用它。

module Main where

import Data.Natural

main = putStrLn "Hello World"

When I try to ghc Main.hs I see the following : 当我尝试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.

With the verbose flag enabled, it seems as if my cabal is getting shadowed by a later cabal, which is in turn shadowing the earlier cabal. 随着启用详细标记,似乎我的阴谋被后来的阴影所遮蔽,这反过来影响了早期的阴谋集团。 Why is this happening? 为什么会这样?

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

If you're into manual hacking, you can pass to ghc the sandbox's pkg db location, such as: 如果您正在进行手动黑客攻击,则可以将沙箱的pkg db位置传递给ghc,例如:

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

However the "normal" way to use sandboxes is to always compile using cabal build (or cabal install with no parameters) rather than running ghc directly. 但是,使用沙箱的“常规”方法是始终使用cabal build (或没有参数的cabal install进行编译,而不是直接运行ghc。

  1. Run cabal init , answer questions as you please 运行cabal initcabal init回答问题
  2. Edit the resulting foo.cabal file ( foo is the name of your package). 编辑生成的foo.cabal文件( foo是包的名称)。
  3. Run cabal build or cabal install - this will run ghc for you. 运行cabal buildcabal install - 这将为你运行ghc。

When editing the cabal file, check that your exported modules (if it's a lib) are listed and that your main src is correct. 编辑cabal文件时,检查导出的模块(如果是lib)是否已列出,并且主src是否正确。 Also make sure dependencies such as natural-numbers are listed in the build-depends: clause. 还要确保build-depends:子句中列出了诸如natural-numbersbuild-depends:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM