Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为Haskell语言的程序生成Makefile?_Haskell_Makefile_Ghci - Fatal编程技术网

如何为Haskell语言的程序生成Makefile?

如何为Haskell语言的程序生成Makefile?,haskell,makefile,ghci,Haskell,Makefile,Ghci,我是Haskell语言的新手,我想为我正在编写的Haskell程序创建一个makefile。但我不知道该怎么做 谢谢:)几乎没有Haskell项目使用Makefile,因为Makefile很笨重,很难阅读,而且容易出错 对于简单的一次性项目,您根本不需要任何专用的构建系统:GHC本身可以找出模块之间的依赖关系: sagemuej@sagemuej-X302LA:/tmp$ cat >> Hello.hs module Main where import Greeting main

我是Haskell语言的新手,我想为我正在编写的Haskell程序创建一个makefile。但我不知道该怎么做


谢谢:)

几乎没有Haskell项目使用Makefile,因为Makefile很笨重,很难阅读,而且容易出错

对于简单的一次性项目,您根本不需要任何专用的构建系统:GHC本身可以找出模块之间的依赖关系:

sagemuej@sagemuej-X302LA:/tmp$ cat >> Hello.hs
module Main where

import Greeting

main = putStrLn greeting



…或者干脆

sagemuej@sagemuej-X302LA:/tmp$ runhaskell Hello.hs
Hello, World!
对于更复杂的项目和库,您希望使用。也就是说,您需要一个
.cabal
文件,例如,对于上述“项目”
hello.cabal
。这样的文件可以通过
cabal
工具半自动生成:

sagemuej@sagemuej-X302LA:/tmp$ mkdir hello
sagemuej@sagemuej-X302LA:/tmp$ mv Hello.hs hello
sagemuej@sagemuej-X302LA:/tmp$ mv Greeting.hs hello
sagemuej@sagemuej-X302LA:/tmp$ cd hello/
sagemuej@sagemuej-X302LA:/tmp/hello$ cabal init
Package name? hello
This package name is already used by another package on hackage. Do you want to choose a different name? [default: y] n
Package version? [default: 0.1.0.0] 
Please choose a license:
   1) GPL-2
   2) GPL-3
   3) LGPL-2.1
   4) LGPL-3
   5) AGPL-3
   6) BSD2
 * 7) BSD3
   8) MIT
   9) ISC
  10) MPL-2.0
  11) Apache-2.0
  12) PublicDomain
  13) AllRightsReserved
  14) Other (specify)
Your choice? [default: BSD3] 12
Author name? [default: Justus Sagemüller] 
Maintainer email? [default: sagemueller@geo.uni-koeln.de] 
Project homepage URL? 
Project synopsis? hello
Project category:
 * 1) (none)
   2) Codec
   3) Concurrency
   4) Control
   5) Data
   6) Database
   7) Development
   8) Distribution
   9) Game
  10) Graphics
  11) Language
  12) Math
  13) Network
  14) Sound
  15) System
  16) Testing
  17) Text
  18) Web
  19) Other (specify)
Your choice? [default: (none)] 17
What does the package build:
   1) Library
   2) Executable
Your choice? 2
What is the main module of the executable:
 * 1) Main.hs (does not yet exist, but will be created)
   2) Main.lhs (does not yet exist, but will be created)
   3) Other (specify)
Your choice? [default: Main.hs (does not yet exist, but will be created)] 3
Please specify? Hello.hs
Source directory:
 * 1) (none)
   2) src
   3) Other (specify)
Your choice? [default: (none)] 
What base language is the package written in:
 * 1) Haskell2010
   2) Haskell98
   3) Other (specify)
Your choice? [default: Haskell2010] 
Add informative comments to each field in the cabal file (y/n)? [default: n] 

Guessing dependencies...
Generating Setup.hs...
Generating ChangeLog.md...
Generating hello.cabal...

You may want to edit the .cabal file and add a Description field.
此时,该文件将包含:

sagemuej@sagemuej-X302LA:/tmp/hello$ cat hello.cabal 
-- Initial hello.cabal generated by cabal init.  For further documentation,
--  see http://haskell.org/cabal/users-guide/

name:                hello
version:             0.1.0.0
synopsis:            hello
-- description:         
license:             PublicDomain
author:              Justus Sagemüller
maintainer:          sagemueller@geo.uni-koeln.de
category:            Text
build-type:          Simple
extra-source-files:  ChangeLog.md
cabal-version:       >=1.10

executable hello
  main-is:             Hello.hs
  -- other-modules:       
  -- other-extensions:    
  build-depends:       base >=4.10 && <4.11
  -- hs-source-dirs:      
  default-language:    Haskell2010
但就这样,你现在可以做了

sagemuej@sagemuej-X302LA:/tmp/hello$ cabal build
Resolving dependencies...
Configuring hello-0.1.0.0...
Preprocessing executable 'hello' for hello-0.1.0.0..
Building executable 'hello' for hello-0.1.0.0..
[1 of 2] Compiling Greeting         ( Greeting.hs, dist/build/hello/hello-tmp/Greeting.o )
[2 of 2] Compiling Main             ( Hello.hs, dist/build/hello/hello-tmp/Main.o )
Linking dist/build/hello/hello ...

或者,只需运行脚本,
cabal run
,或
cabal install
,即可使其始终在您的系统上可用。

我们知道您想要什么。伟大的到目前为止你有什么?我不建议你这么做。相反,使用
ghc--make
,或
cabal
,或
stack
。如果您确实需要
make
,请阅读相关文档:首先,您需要考虑在制作程序时自己调用什么命令,然后将其转换为Makefile。如果您的程序位于
C
中,您将使用
gcc-C
,大多数Haskell程序都是使用ghc编译的,因此您使用
ghc-C
。如果您想要优化,那么在C中您可以使用
-O2
或3,在ghc中您也可以使用
-O2
。如果您想在C语言中使用警告,请使用
-Wall
,ghc则使用
-Wall
。如果你想用c编译一个二进制文件,你只需去掉标志并调用
gcc
,而用Haskell你只需去掉标志并使用
ghc
。如果您想用C从多个目标文件链接一个程序,可以使用编译器或链接器,例如
gcc obj1.o obj2.o-o res
;使用ghc,您可以调用ghc
ghc obj1.o obj2.o-o res
。问题?
sagemuej@sagemuej-X302LA:/tmp$ mkdir hello
sagemuej@sagemuej-X302LA:/tmp$ mv Hello.hs hello
sagemuej@sagemuej-X302LA:/tmp$ mv Greeting.hs hello
sagemuej@sagemuej-X302LA:/tmp$ cd hello/
sagemuej@sagemuej-X302LA:/tmp/hello$ cabal init
Package name? hello
This package name is already used by another package on hackage. Do you want to choose a different name? [default: y] n
Package version? [default: 0.1.0.0] 
Please choose a license:
   1) GPL-2
   2) GPL-3
   3) LGPL-2.1
   4) LGPL-3
   5) AGPL-3
   6) BSD2
 * 7) BSD3
   8) MIT
   9) ISC
  10) MPL-2.0
  11) Apache-2.0
  12) PublicDomain
  13) AllRightsReserved
  14) Other (specify)
Your choice? [default: BSD3] 12
Author name? [default: Justus Sagemüller] 
Maintainer email? [default: sagemueller@geo.uni-koeln.de] 
Project homepage URL? 
Project synopsis? hello
Project category:
 * 1) (none)
   2) Codec
   3) Concurrency
   4) Control
   5) Data
   6) Database
   7) Development
   8) Distribution
   9) Game
  10) Graphics
  11) Language
  12) Math
  13) Network
  14) Sound
  15) System
  16) Testing
  17) Text
  18) Web
  19) Other (specify)
Your choice? [default: (none)] 17
What does the package build:
   1) Library
   2) Executable
Your choice? 2
What is the main module of the executable:
 * 1) Main.hs (does not yet exist, but will be created)
   2) Main.lhs (does not yet exist, but will be created)
   3) Other (specify)
Your choice? [default: Main.hs (does not yet exist, but will be created)] 3
Please specify? Hello.hs
Source directory:
 * 1) (none)
   2) src
   3) Other (specify)
Your choice? [default: (none)] 
What base language is the package written in:
 * 1) Haskell2010
   2) Haskell98
   3) Other (specify)
Your choice? [default: Haskell2010] 
Add informative comments to each field in the cabal file (y/n)? [default: n] 

Guessing dependencies...
Generating Setup.hs...
Generating ChangeLog.md...
Generating hello.cabal...

You may want to edit the .cabal file and add a Description field.
sagemuej@sagemuej-X302LA:/tmp/hello$ cat hello.cabal 
-- Initial hello.cabal generated by cabal init.  For further documentation,
--  see http://haskell.org/cabal/users-guide/

name:                hello
version:             0.1.0.0
synopsis:            hello
-- description:         
license:             PublicDomain
author:              Justus Sagemüller
maintainer:          sagemueller@geo.uni-koeln.de
category:            Text
build-type:          Simple
extra-source-files:  ChangeLog.md
cabal-version:       >=1.10

executable hello
  main-is:             Hello.hs
  -- other-modules:       
  -- other-extensions:    
  build-depends:       base >=4.10 && <4.11
  -- hs-source-dirs:      
  default-language:    Haskell2010
...
executable hello
  main-is:             Hello.hs
  other-modules:       Greeting
  -- other-extensions:
...
sagemuej@sagemuej-X302LA:/tmp/hello$ cabal build
Resolving dependencies...
Configuring hello-0.1.0.0...
Preprocessing executable 'hello' for hello-0.1.0.0..
Building executable 'hello' for hello-0.1.0.0..
[1 of 2] Compiling Greeting         ( Greeting.hs, dist/build/hello/hello-tmp/Greeting.o )
[2 of 2] Compiling Main             ( Hello.hs, dist/build/hello/hello-tmp/Main.o )
Linking dist/build/hello/hello ...