[Haskell-beginners] cabal: library and executable

Ertugrul Söylemez es at ertes.de
Thu May 9 18:10:55 CEST 2013


harry <voldermort at hotmail.com> wrote:

> I've written a library, and a simple program to demonstrate it. I can
> write the cabal file for the library, but I can't get the executable
> section right. How can I tell it to use the library defined herein?

You need two things for this to work properly.  Firstly you need to
require a sufficiently recent Cabal version, for example:

    name:          mylib
    version:       0.1.0
    cabal-version: >= 1.10

    library
        -- ...

Secondly to avoid double-compilation you should put your program's
source code in a different tree.  My personal approach is to put the
library's source code right within the project's root directory, but the
executable's source code below a subdirectory called 'program', so the
Main module is in 'program/Main.hs'.  Then just mention the package
itself as a dependency:

    executable myprog
        build-depends:
            base >= 4.5 && < 5,
            mylib
        default-language: Haskell2010
        ghc-options: -W -threaded -rtsopts
        hs-source-dirs: program
        main-is: Main.hs

If the program is just a demo that you wouldn't normally want to install
with the library, you can make it a test suite instead of an executable:

    test-suite mylib-demo
        type: exitcode-stdio-1.0
        build-depends:
            base >= 4.5 && < 5,
            mylib
        default-language: Haskell2010
        ghc-options: -W -threaded -rtsopts
        hs-source-dirs: program
        main-is: Main.hs

That way the executable is only built when you configure with
--enable-tests and isn't installed together with the library.  However,
keep in mind that if you have an actual test suite the demo program will
be run along with it when you command 'cabal test':

    test-suite mylib-props
        -- ...

To avoid that just invoke your test suite on the command line instead of
through 'cabal test':

    ln -s dist/build/tests/tests
    ./tests


Greets,
Ertugrul

-- 
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130509/a7c7dd34/attachment.pgp>


More information about the Beginners mailing list