如何使用 stack 和 cabal 安装 Haskell 包?

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

我正在尝试安装一个小型 Haskell 软件包。

我使用

stack
设置了一个项目:

stack new my-project simple

效果很好。我能够构建并运行代码。

但后来我偶然发现需要使用其他一些库。在互联网上搜索,我发现我应该使用

cabal
来做到这一点。第一次安装:

brew install cabal-install

然后更新了:

cabal update

然后尝试用它安装一个库:

cabal install --lib split

这导致:

Warning: Unknown/unsupported 'ghc' version detected (Cabal 3.10.1.0 supports
'ghc' version < 9.8): /usr/local/bin/ghc is version 9.8.1
Warning: Unknown/unsupported 'ghc' version detected (Cabal 3.10.1.0 supports
'ghc' version < 9.8): /usr/local/bin/ghc is version 9.8.1
Resolving dependencies...
Build profile: -w ghc-9.8.1 -O1
In order, the following will be built (use -v for more details):
 - split-0.2.4 (lib) (requires download & build)
Downloading  split-0.2.4
Downloaded   split-0.2.4
Starting     split-0.2.4 (lib)
Building     split-0.2.4 (lib)

Failed to build split-0.2.4.
Build log ( /Users/nene/.cache/cabal/logs/ghc-9.8.1/splt-0.2.4-d1ef7190.log ):
Configuring library for split-0.2.4..
Preprocessing library for split-0.2.4..
Building library for split-0.2.4..
[1 of 2] Compiling Data.List.Split.Internals ( src/Data/List/Split/Internals.hs, dist/build/Data/List/Split/Internals.o, dist/build/Data/List/Split/Internals.dyn_o )
[2 of 2] Compiling Data.List.Split  ( src/Data/List/Split.hs, dist/build/Data/List/Split.o, dist/build/Data/List/Split.dyn_o )
ld: warning: directory not found for option '-L/Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/lib'
ld: unknown option: -no_fixup_chains
clang: error: linker command failed with exit code 1 (use -v to see invocation)
`clang' failed in phase `Linker'. (Exit code: 1)
Error: cabal: Failed to build split-0.2.4. See the build log above for
details.

似乎与 Cabal 版本(3.10.1.0)和 GHC 版本(9.8.1)有冲突。不知道如何解决。

此外,我还有一个

my-project.cabal
创建的
stack
文件。在里面我看到:

cabal-version:       2.2

如果我的思维模型是正确的,那么

stack
使用自己单独的 GHC 安装以及(可能)自己的 Cabal 安装。但我不知道如何访问这些。

我尝试创建

package.yaml
文件(按照 Silvio Mayolo 的建议),内容如下:

dependencies:
- split

但这似乎没有效果。构建只是抱怨它无法找到

Data.List.Split
模块(
split
库应该提供)。

PS: 现在我已经简单地自己实现了我想要导入的小功能。但如果能弄清楚如何安装一些 Haskell 库那就太好了。

haskell cabal haskell-stack
1个回答
0
投票

要将依赖项添加到使用简单模板创建的项目,您需要将其添加到由模板创建的

build-depends
文件中的
.cabal
字段 - 在您的情况下为
my-project.cabal

之后,你的 cabal 文件应该如下所示:

cabal-version:       2.2

name:                my-project
version:             0.1.0.0
-- synopsis:
-- description:
homepage:            https://github.com/githubuser/my-project#readme
license:             BSD-3-Clause
license-file:        LICENSE
author:              Author name here
maintainer:          [email protected]
copyright:           2023 Author name here
category:            Web
build-type:          Simple
extra-source-files:  README.md
                     CHANGELOG.md

executable my-project
  hs-source-dirs:      src
  main-is:             Main.hs
  default-language:    Haskell2010
  build-depends:       base >= 4.7 && < 5, split
  ghc-options:         -Wall
                       -Wcompat
                       -Widentities
                       -Wincomplete-record-updates
                       -Wincomplete-uni-patterns
                       -Wmissing-export-lists
                       -Wmissing-home-modules
                       -Wpartial-fields
                       -Wredundant-constraints
© www.soinside.com 2019 - 2024. All rights reserved.