在cabal文件的库或可执行部分中建立依赖关系? [英] Build dependency in library or executable section of cabal file?

查看:44
本文介绍了在cabal文件的库或可执行部分中建立依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是Haskell的阴谋和外部软件包的新手.

First off, I'm new to using cabal and external packages with Haskell.

我正在尝试在MyLib中使用Graphics.Gloss包.如果我在 library executable build-depends 中都包含了 gloss ,则可以使其工作.

I'm trying to use the Graphics.Gloss package inside of MyLib. I can make it work if I include gloss in both the build-depends of library and executable.

以下是阴谋文件的相关部分:

Here is the relevant portion of the cabal file:

library
  exposed-modules:     MyLib
  build-depends:       base ^>=4.13.0.0,
                       gloss ^>=1.13.1.1
  default-language:    Haskell2010

executable ray-tracer
  main-is:             Main.hs
  other-modules:       MyLib
  build-depends:       base ^>=4.13.0.0, ray-tracer,
                       haskell-say ^>=1.0.0.0,
                       gloss ^>=1.13.1.1

MyLib.hs

module MyLib (someFunc) where
import Graphics.Gloss

someFunc :: IO ()
someFunc = display (InWindow "My Window" (200,200) (10,10)) white (Circle 80)

Main.hs

module Main where

import qualified MyLib (someFunc)
import HaskellSay (haskellSay)

main :: IO ()
main = do
  MyLib.someFunc

当仅在 library 依赖项中包含 gloss 时,为什么这不起作用?

Why doesn't this work when gloss is only included in the library dependencies?

推荐答案

您可以使其正常运行.您当前的设置存在一个问题,那就是库文件和可执行文件位于同一目录中.另请参阅此问题如何避免在此阴谋文件中重新编译?是相同潜在问题的征兆:构建可执行文件时,它从头开始构建 MyLib (这需要光泽依赖),而不是重新使用已构建的库.

You can make it work. There is a problem in your current set up, which is that the files for the library and the executable are in the same directory. See also this question How to avoid recompiling in this cabal file? which is a symptom of the same underlying problem: when you build the executable, it rebuilds MyLib from scratch (which requires the gloss dependency) instead of reusing your library that was already built.

MyLib/
  ray-tracer.cabal
  MyLib.hs
  Main.hs       # Bad

.hs 文件移动到单独的目录中(从技术上讲,您只需要移动其中之一,但是我认为最好使根目录尽可能统一):

Move the .hs files in separate directories (technically you only need to move one of them, but I think it's better to keep the root directory as uniform as possible):

MyLib/
  MyLib.cabal
  src/
    MyLib.hs
  exe/
    Main.hs

然后在cabal文件中,将 hs-source-dirs:src hs-source-dirs:exe 添加到相应的部分:

And in the cabal file, add hs-source-dirs: src and hs-source-dirs: exe to the corresponding sections:

library
  hs-source-dirs: src
  ...

executable ray-tracer
  hs-source-dirs: exe
  ...

这篇关于在cabal文件的库或可执行部分中建立依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆