Home > Enterprise >  How do I link cabal library and executable?
How do I link cabal library and executable?

Time:09-17

cabal-version:      3.4
name:               SudokuSolver
version:            0.1.0.0
build-type:         Simple

library sud
    build-depends:
            base
          , text
    hs-source-dirs: lib
    exposed-modules: Sud.Rdg
    default-language: Haskell2010

executable suSol
    default-language: Haskell2010
    hs-source-dirs: app
    main-is: Main.hs
    build-depends:    
            base
          , optparse-applicative
          , filepath
          , text

When I run "cabal build sud" all is ok. However when i run "cabal build suSol" I get the following error:

Could not load module ‘Sud.Rdg’ ~ │ It is a member of the hidden package ‘SudokuSolver-0.1.0.0’. ~ │ Perhaps you need to add ‘SudokuSolver’ to the build-depends in your .cabal file. ~ │ Use -v (or :set -v in ghci) to see a list of the files searched for.

This is where I am a bit at a loss, cabal sudgests I add this project itself as build-depends? If I do that I get:

Error: Dependency on unbuildable package SudokuSolver
│ In the stanza 'executable suSol' | In the inplace package 'SudokuSolver-0.1.0.0'

That makes sence to me, obviously you can't add yourself as build-dependencies.

But what am I missing here?

edit:

If i add sud to the build-depends and "cabal build suSol" I get:

Resolving dependencies...
cabal: Could not resolve dependencies: [__0] trying: SudokuSolver-0.1.0.0 (user goal) [__1] unknown package: sud (dependency of SudokuSolver) [__1] fail (backjumping, conflict set: SudokuSolver, sud) After searching the rest of the dependency tree exhaustively, these were the goals I've had most trouble fulfilling: SudokuSolver, sud

CodePudding user response:

When you add a name to a library that means it is an private or internal library of your package. It seems that cabal currently has a bug which causes that confusing error message asking you to add SudokuSolver to your build-depends of your executable, you could instead add SudokuSolver:sud.

However, it is much more common too keep the library unnamed (or give it the same name as the package), which makes it the main public library. Then the name of the library is the same as the name of the package so you should really add SudokuSolver to the build-depends of your executable. This was the only possible option for a long time, internal libraries are relatively new. That is probably also why there are still a few issues like this.

I also notice that you can add sud as Willem van Onsem says, but only if you use cabal-version: 3.0 or earlier. With cabal-version: 3.4 you have to use the SudokuSolver:sud (package-name:internal-library-name) notation.

Edit: I have opened an issue on the cabal GitHub repo.

  • Related