简体   繁体   中英

Compile multi .hs files of Haskell - Unix

I created some files from a project in Unix, they are a lot and if I want to execute it in another pc or folder I need to copy all files to there. They are all connected in import . How can I make an executable haskell program?

I have per example:

main.hs - main where all the program executes; using,besides haskell, unix shell.
ex1.hs - basically types of data, some functions.
ex2.lhs - same as ex1.lhs but is literate with LaTeX
pic.jpg - picture to use on the pdflatex
package.sty - package needed to use some functions

How do I proceed and compile all of these? I tried using ghc but always giving errors:

>ghc -o MAIN main.hs ex1.hs ex2.lhs pic.jpg package.sty

Failed to load interface for 'ex1.hs' And is in the line which has import ex1.hs

Curious is if I trade import ex1.hs to import ex2.lhs line will give error on ex2

  1. Haskell module names must begin with an upper case letter, so start by renaming ex1.hs and ex2.hs to Ex1.hs and Ex2.hs . They should also start with module Ex1 where , otherwise the module name will default to Main , and GHC will be very confused when the module names don't match the file names.

  2. Import statements should refer to the module name, not the file name, so change them in main.hs to correspond to the module names.

     import Ex1 import Ex2 
  3. Now compile with ghc --make main.hs , and it should find the other modules automatically. It will search for modules with both .hs and .lhs extensions, and correctly treat the latter like literate Haskell files.

For larger projects, you should look into using Cabal , the build system used by most Haskell libraries and programs. It will help with managing dependencies and compiler options, sort of like a Makefile.

Try

ghc --make main.hs

That should make ghc try to do everything it can...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM