简体   繁体   English

在 GHC 中编译

[英]Compiling in GHC

I'm learning Haskell and a simple doubt struck me, what is the difference between these two commands to compile using GHC (fac is a program to calculate factorial) :我正在学习 Haskell,一个简单的疑问让我感到震惊,这两个使用 GHC 编译的命令之间有什么区别(fac 是一个计算阶乘的程序):

ghc -o fac fac.hs ghc -o fac fac.hs

and

ghc fac.hs ghc fac.hs

Now to run the program I just do fac.exe (I'm using Windows).现在要运行该程序,我只需执行 fac.exe(我使用的是 Windows)。

Given that both works which one is right?鉴于两者都有效,哪一个是正确的? both?两个都? And which one is the most advisable to use?最好使用哪一种?

Both are OK.两者都可以。 The only thing the -o command does is determine the name of the executable file that is generated. -o 命令所做的唯一一件事就是确定生成的可执行文件的名称。 As far as the Haskell goes it doesn't make any difference.就 Haskell 而言,它没有任何区别。

Personally, I like always using "-o" to make things explicit (since I like avoiding implicit defaults in general) and to make it look more similar to other compilers (for example, the gcc C compiler kind of forces you to use its "-o" option, since the default name it uses otherwise is very ugly)就我个人而言,我喜欢总是使用“-o”来使事情变得明确(因为我一般喜欢避免隐式默认值)并使其看起来与其他编译器更相似(例如,gcc C 编译器强制您使用它的“ -o" 选项,因为它使用的默认名称否则非常难看)

It used to be that writing以前是这样写的

ghc fac.hs

didn't really work.没有真正起作用。 Instead you had to say相反,你不得不说

ghc --make fac.hs

The difference is that the former compiles fac.hs and does nothing else, while the latter checks to see if fac.hs uses any other modules that need recompiling, automatically includes any packages required, and so on and so forth.区别在于前者编译fac.hs不做任何其他事情,而后者检查fac.hs使用任何其他需要重新编译的模块,自动包含所需的任何包,等等。

However, the default behavior has been changed, and the above two commands are now identical.但是,默认行为已更改,上面的两个命令现在是相同的。 (I forget exactly when this happened, but it was a while ago now.) You may still see documentation and examples with the explicit --make switch though, and personally I like to include it. (我忘记了这到底是什么时候发生的,但现在已经有一段时间了。)尽管如此,您仍然可以看到带有显式--make开关的文档和示例,我个人喜欢将其包含在内。 But that's just me.但这只是我。

The only thing the -o switch gives you is the ability to change the output name. -o开关为您提供的唯一功能是更改输出名称的能力。 This can be useful if you have, say, Main.hs and you want the output to be called MyCoolProgram or whatever.如果您有Main.hs并且您希望将输出称为MyCoolProgram或其他任何内容,这将很有用。 One thing I will say: under Linux, fac.hs (if it's the Main module) will compile to an executable named fac , but under Windows it will be called fac.exe .有一件事我说:在Linux下, fac.hs (如果它是Main模块)将编译为可执行文件名为fac ,但在Windows下它会被称为fac.exe If you use -o to hard-code the binary name, this won't happen.如果您使用-o对二进制名称进行硬编码,则不会发生这种情况。 And I'm not sure you can run a Windows binary that doesn't end .exe ...而且我不确定您是否可以运行不以.exe结尾的 Windows 二进制文件...

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

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