You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Haskell中导入Control.Lens及解决cabal install lens报错的方法

解决 Cabal 安装 lens 包时的“无执行文件”错误

我刚接触Haskell时也踩过这个坑,别慌,问题出在你用错了Cabal的用法——lens是一个纯库包,本身不带可执行程序,所以直接跑cabal install lens会触发这个错误。正确的做法是把它添加到你的项目依赖中,具体步骤如下:

步骤1:修改项目的.cabal配置文件

打开项目根目录下的.cabal文件,找到对应可执行目标的build-depends字段,把lens添加进去。举个我自己的配置示例:

executable Prelude3
main-is: Main.hs
-- Modules included in this executable, other than Main.
--other-modules: Basic
-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
build-depends: containers,lens,microlens,microlens-platform,base ^>=4.14.3.0
hs-source-dirs: app
default-language: Haskell2010

注意要保证build-depends里的包版本和你当前的base版本兼容,上面的base ^>=4.14.3.0对应我使用的GHC版本,你可以根据自己的环境调整。

步骤2:在代码中导入Control.Lens

打开你的Main.hs文件,在顶部加入导入语句:

import Control.Lens

步骤3:构建并运行项目

回到终端,在项目根目录下执行以下命令:

cabal build
cabal run

这时候Cabal会自动下载、编译lens包及其依赖,之后你就能正常使用Control.Lens里的功能了。


内容的提问来源于stack exchange,提问作者Mahmut Salman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 21:43:10