简体   繁体   English

将 windows 中的 Haskell (.hs) 编译为 exe

[英]Compiling Haskell (.hs) in windows to a exe

Is it possible to compile a set of .hs haskell files into a exe in windows?是否可以将一组.hs haskell文件编译成 windows 中的 exe? .hs -> .exe .hs -> .exe

Absolutely.绝对地。 Install the Haskell Platform , which gives you GHC , a state-of-the-art compiler for Haskell.安装Haskell 平台,它为您提供GHC ,这是 Haskell 的最先进的编译器。

By default it compiles to executables, and it works the same on Linux or Windows.默认情况下,它编译为可执行文件,并且在 Linux 或 Windows 上的工作方式相同。 Eg例如

Given a file:给定一个文件:

$ cat A.hs
main = print "hello, world"

Compile it with GHC:用 GHC 编译它:

$ ghc --make A.hs
[1 of 1] Compiling Main             ( A.hs, A.o )
Linking A.exe ...

Which you can now run:您现在可以运行:

$ ./A.exe
"hello, world"

Note, this was under Cygwin.注意,这是在 Cygwin 下。 But the same holds for native Windows:但同样适用于原生 Windows:

C:\temp>ghc --make A.hs
[1 of 1] Compiling Main             ( A.hs, A.o )
Linking A.exe ...

C:\temp>A.exe
"hello, world"

For people who never compiled anything at all, it also might be useful to know that "C:\temp>" in the example by Don Stewart points to the folder in which.hs should be.对于从未编译过任何东西的人来说,知道 Don Stewart 示例中的“C:\temp>”指向应包含.hs 的文件夹也可能很有用。 For example, if you have a folder under your user account, say "C:\Users\Username\Haskell\" in which you have your hello.hs file, you open command prompt by typing cmd, when it opens, you'll see "C:\Users\Username>".例如,如果您的用户帐户下有一个文件夹,例如“C:\Users\Username\Haskell\”,其中有您的 hello.hs 文件,您可以通过键入 cmd 打开命令提示符,当它打开时,您将请参阅“C:\用户\用户名>”。 To compile you file type the following:要编译您的文件,请键入以下内容:

ghc Haskell\hello.hs

So the entire line should look like:所以整行应该是这样的:

C:\Users\Username>ghc Haskell\hello.hs

Provided you have no any mistypes, you should be able to see the result in the same folder as you have your hello.hs file.如果您没有任何错误输入,您应该能够在与您的 hello.hs 文件相同的文件夹中看到结果。

Yes.是的。 GHC can compile to C which can then be compiled to native machine code, or it can compile to LLVM. GHC可以编译为 C,然后可以编译为本地机器码,也可以编译为 LLVM。

You should simply install the Haskell Platform , including GHC and the IDE Leksah.您只需安装Haskell 平台,包括 GHC 和 IDE Leksah。 Using this environment compilation becomes very easy and convenient.使用这个环境编译变得非常简单方便。

On Windows (without using Cygwin) there are two steps to creating the.exe file from the.hs file:在 Windows(不使用 Cygwin)上,从 .hs 文件创建 .exe 文件有两个步骤:

(1) You need to navigate in the command line terminal to the folder containing your.hs file. (1) 您需要在命令行终端中导航到包含 your.hs 文件的文件夹。 To do so, you can either do:为此,您可以执行以下任一操作:

  • Go to Start >> Search programs and filles >> Type in "cmd" to launch the command line terminal, type "cd PATH " (where PATH is the correct file path to the folder where your.hs file is located), or Go 开始>>搜索程序和填充>>键入“cmd”以启动命令行终端,键入“cd PATH ”(其中PATH是your.hs文件所在文件夹的正确文件路径),或
  • You can manually navigate in your File Explorer to the specific folder where your.hs file is located, hold down the "Shift" key, right click within the folder, and select "open command window here."您可以在文件资源管理器中手动导航到 your.hs 文件所在的特定文件夹,按住“Shift”键,在文件夹中单击鼠标右键,然后 select“在此处打开命令 window”。 This will open up a command line terminal in the correct file location.这将在正确的文件位置打开一个命令行终端。

(2) You need to compile the.hs file. (2) 需要编译.hs文件。 To do so, you can type after the prompt either:为此,您可以在提示后键入:

  • ghc hello.hs or ghc hello.hs
  • ghc --make hello

Either option will produce a hello.exe executable file in the same folder as your hello.hs file.任一选项都会在与 hello.hs 文件相同的文件夹中生成一个 hello.exe 可执行文件。

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

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