无法在Haskell中导入模块

问题描述 投票:2回答:1

您好我正在尝试使用Haskell与vscode,如果我尝试在另一个内部使用模块我得到以下错误:

Failed to load interface for **Client**
Use -v to see a list of the files searched for.

第一个模块:

module Main where 

import qualified Client as Cl

main ::IO ()
main = do
name<- getLine
sp

第二个模块:

    module Client 
    (sp)where

    sp::IO()
    sp=print "SecondModule"

经过一些阅读后,我尝试更新堆栈并安装构建工具,此时我收到以下错误:

   $ stack install gtk2hs-buildtools
    ←[0mprimitive-0.6.3.0: using precompiled package←[0m
    ←[0mCabal-2.0.1.1: using precompiled package←[0m
    ←[0mmtl-2.2.1: using precompiled package←[0m
    ←[0mrandom-1.1: using precompiled package←[0m
    ←[0mtext-1.2.2.2: using precompiled package←[0m
    ←[0mvector-0.12.0.1: using precompiled package←[0m
    ←[0mtf-random-0.5: configure←[0m
    ←[0mhashable-1.2.6.1: using precompiled package←[0m
    ←[0mhappy-1.19.9: download←[0m
    ←[0mtf-random-0.5: build←[0m
    ←[0mhashtables-1.2.2.1: download←[0m
    ←[0mhashtables-1.2.2.1: configure←[0m
    ←[0mhashtables-1.2.2.1: build←[0m
    ←[0mhappy-1.19.9: configure←[0m
    ←[0mtf-random-0.5: copy/register←[0m
    ←[0mQuickCheck-2.10.1: download←[0m
    ←[0mhappy-1.19.9: build←[0m
    ←[0mQuickCheck-2.10.1: configure←[0m
    ←[0mQuickCheck-2.10.1: build←[0m
    ←[0mhashtables-1.2.2.1: copy/register←[0m
    ←[0mQuickCheck-2.10.1: copy/register←[0m
    ←[0malex-3.2.3: download←[0m
    ←[0malex-3.2.3: configure←[0m
    ←[0malex-3.2.3: build←[0m
    ←[0mhappy-1.19.9: copy/register←[0m
    ←[0malex-3.2.3: copy/register←[0m
    ←[0mgtk2hs-buildtools-0.13.3.1: download←[0m
    ←[0mgtk2hs-buildtools-0.13.3.1: configure←[0m
    ←[0mgtk2hs-buildtools-0.13.3.1: build←[0m
    ←[0mgtk2hs-buildtools-0.13.3.1: copy/register←[0m
    Completed 13 action(s).
    removeDirectoryRecursive:removeContentsRecursive:removePathRecursive:removeContentsRecursive:removePathRecursive:removeContentsRecursive:removePathRecursive:removeContentsRecursive:removePathRecursive:removeContentsRecursive:removePathRecursive:removeContentsRecursive:removePathRecursive:DeleteFile "C:\\Users\\aita\\AppData\\Local\\Temp\\stack7744\\gtk2hs-buildtools-0.13.3.1\\.stack-work\\dist\\5c8418a7\\build\\gtk2hsC2hs\\gtk2hsC2hs.exe": permission denied (The process cannot access the file because it is being used by another process.)

我小心不要让任何其他进程使用此文件夹,但我不知道为什么错误。

这是我的stack.yaml文件:

# This file was automatically generated by 'stack init'
#
# Some commonly used options have been documented as comments in this file.
# For advanced use and comprehensive documentation of the format, please see:
# https://docs.haskellstack.org/en/stable/yaml_configuration/

# Resolver to choose a 'specific' stackage snapshot or a compiler version.
# A snapshot resolver dictates the compiler version and the set of packages
# to be used for project dependencies. For example:
#
# resolver: lts-3.5
# resolver: nightly-2015-09-21
# resolver: ghc-7.10.2
# resolver: ghcjs-0.1.0_ghc-7.10.2
# resolver:
#  name: custom-snapshot
#  location: "./custom-snapshot.yaml"
resolver: lts-10.7

# User packages to be built.
# Various formats can be used as shown in the example below.
#
# packages:
# - some-directory
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
# - location:
#    git: https://github.com/commercialhaskell/stack.git
#    commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a
#   extra-dep: true
#  subdirs:
#  - auto-update
#  - wai
#
# A package marked 'extra-dep: true' will only be built if demanded by a
# non-dependency (i.e. a user package), and its test suites and benchmarks
# will not be run. This is useful for tweaking upstream packages.
packages:
- .
# Dependency packages to be pulled from upstream that are not in the resolver
# (e.g., acme-missiles-0.3)
# extra-deps: []

# Override default flag values for local packages and extra-deps
# flags: {}

# Extra package databases containing global packages
# extra-package-dbs: []

# Control whether we use the GHC we find on the path
# system-ghc: true
#
# Require a specific version of stack, using version ranges
# require-stack-version: -any # Default
# require-stack-version: ">=1.6"
#
# Override the architecture used by stack, especially useful on Windows
# arch: i386
# arch: x86_64
#
# Extra directories used by stack for building
# extra-include-dirs: [/path/to/dir]
# extra-lib-dirs: [/path/to/dir]
#
# Allow a newer minor version of GHC than the snapshot specifies
# compiler-check: newer-minor
Main
Client

Cabal文件。

name:                Hworld
version:             0.1.0.0
-- synopsis:
-- description:
homepage:            https://github.com/githubuser/Hworld#readme
license:             BSD3
license-file:        LICENSE
author:              Author name here
maintainer:          [email protected]
copyright:           2018 Author name here
category:            Web
build-type:          Simple
cabal-version:       >=1.10
extra-source-files:  README.md

executable Hworld
  hs-source-dirs:      src
  main-is:             Main.hs
  default-language:    Haskell2010
  build-depends:       base >= 4.7 && < 5
haskell visual-studio-code haskell-stack
1个回答
4
投票

首先,stack install有点用词不当。与npm installbower install不同,它不会下载任何内容或“添加”任何项目。在stack下,所有包都(可能,逻辑上)一直下载和安装。 stack将在第一次使用时构建一个包,但没有必要明确要求stack下载它。

stack install实际上做了什么,它构建了包,如果包中包含任何可执行文件,它将它们放在PATH上的文件系统上的特殊位置,以便现在可以运行这些可执行文件。看到你如何总是使用stack exec package运行可执行文件而不安装它们,使用stack install通常是不鼓励的,除非你真的确定它是必要的。

要将一个包添加到项目中,您需要将它包含在.cabal部分的build-depends文件中或package.yaml部分的dependencies文件中(无论您使用哪个)。 Here is an example of a package.yamlhere's an example of a .cabal file

但是,您似乎要做的是引用您自己的模块,而不是外部依赖项。为此,stack install更远离事实。

要引用您自己的模块,您需要做两件事:(1)正确的文件系统结构和(2)将它们添加到.cabalpackage.yaml

文件系统结构:在Haskell中(或者更确切地说,在GHC中),模块必须位于文件系统上与其层次名称匹配的位置。例如,模块Main将位于./Main.hs,模块Client将位于./Client.hs,或者说,模块Client.Internals将位于./Client/Internals.hs。有点烦,是的。但这就是Haskell的方式。

Cabal文件:包/库中的所有模块必须在.cabal文件中列出,可以在exposed-modules下或other-modules下。只需将它们添加到那里,编译器就会看到它们。

© www.soinside.com 2019 - 2024. All rights reserved.