Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build dependency in library or executable section of cabal file?

Tags:

haskell

cabal

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

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

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

like image 766
Sevan Golnazarian Avatar asked Jun 09 '20 15:06

Sevan Golnazarian


1 Answers

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

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

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
  ...
like image 107
Li-yao Xia Avatar answered Nov 05 '22 05:11

Li-yao Xia