简体   繁体   English

编译Haskell的多个.hs文件 - Unix

[英]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. 我在Unix中的一个项目中创建了一些文件,它们很多,如果我想在另一个PC或文件夹中执行它,我需要将所有文件复制到那里。 They are all connected in import . 它们都是导入连接的。 How can I make an executable haskell program? 如何制作可执行的haskell程序?

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但总是出错:

>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 Failed to load interface for 'ex1.hs'并且在导入ex1.hs的行中

Curious is if I trade import ex1.hs to import ex2.lhs line will give error on ex2 好奇的是,如果我交易导入ex1.hs 导入ex2.lhs行将在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 . Haskell模块名称必须以大写字母开头,因此首先将ex1.hsex2.hs重命名为Ex1.hsEx2.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. 它们也应该从module Ex1 where开始,否则模块名称将默认为Main ,当模块名称与文件名不匹配时GHC将非常混淆。

  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语句应该引用模块名称,而不是文件名,因此在main.hs更改它们以对应模块名称。

     import Ex1 import Ex2 
  3. Now compile with ghc --make main.hs , and it should find the other modules automatically. 现在用ghc --make main.hs编译,它应该自动找到其他模块。 It will search for modules with both .hs and .lhs extensions, and correctly treat the latter like literate Haskell files. 它将搜索具有.hs.lhs扩展名的模块,并正确地将后者视为有文化的Haskell文件。

For larger projects, you should look into using Cabal , the build system used by most Haskell libraries and programs. 对于大型项目,您应该考虑使用Cabal ,这是大多数Haskell库和程序使用的构建系统。 It will help with managing dependencies and compiler options, sort of like a Makefile. 它将有助于管理依赖项和编译器选项,有点像Makefile。

Try 尝试

ghc --make main.hs

That should make ghc try to do everything it can... 这应该让ghc尝试做它能做的一切......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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