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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6306233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:02:41  来源:igfitidea点击:

Compiling Haskell (.hs) in windows to a exe

windowshaskellcompiler-construction

提问by Sudantha

Is it possible to compile a set of .hshaskellfiles into a exe in windows? .hs-> .exe

是否可以将一组.hshaskell文件编译成Windows中的exe?.hs->.exe

回答by Don Stewart

Absolutely. Install the Haskell Platform, which gives you GHC, a state-of-the-art compiler for Haskell.

绝对地。安装Haskell Platform,它为您提供GHC,Haskell 的最先进的编译器。

By default it compiles to executables, and it works the same on Linux or Windows. E.g.

默认情况下,它编译为可执行文件,并且在 Linux 或 Windows 上的工作方式相同。例如

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. But the same holds for native Windows:

请注意,这是在 Cygwin 下。但同样适用于本机 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"

回答by Kirill G.

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. 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>". To compile you file type the following:

对于从未编译过任何东西的人来说,知道 Don Stewart 示例中的“C:\temp>”指向 .hs 所在的文件夹也可能很有用。例如,如果您的用户帐户下有一个文件夹,请说“C:\Users\Username\Haskell\”,其中包含 hello.hs 文件,您可以通过键入 cmd 打开命令提示符,当它打开时,您将请参见“C:\Users\用户名>”。要编译您的文件,请键入以下内容:

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 文件相同的文件夹中看到结果。

回答by mdm

Yes. GHCcan compile to C which can then be compiled to native machine code, or it can compile to LLVM.

是的。GHC可以编译为 C,然后可以编译为本地机器代码,或者可以编译为 LLVM。

回答by Landei

You should simply install the Haskell Platform, including GHC and the IDE Leksah. Using this environment compilation becomes very easy and convenient.

您应该简单地安装Haskell Platform,包括 GHC 和 IDE Leksah。使用这个环境编译变得非常简单方便。

回答by MLev

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. To do so, you can either do:

(1) 您需要在命令行终端中导航到包含 .hs 文件的文件夹。为此,您可以执行以下任一操作:

  • Go to Start >> Search programs and filles >> Type in "cmd" to launch the command line terminal, type "cd PATH" (where PATHis the correct file path to the folder where your .hs file is located), or
  • 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." This will open up a command line terminal in the correct file location.
  • 转到开始 >> 搜索程序和填充 >> 输入“cmd”以启动命令行终端,输入“cd PATH”(其中PATH是 .hs 文件所在文件夹的正确文件路径),或
  • 您可以在文件资源管理器中手动导航到 .hs 文件所在的特定文件夹,按住“Shift”键,在文件夹内右键单击,然后选择“在此处打开命令窗口”。这将在正确的文件位置打开命令行终端。

(2) You need to compile the .hs file. To do so, you can type after the prompt either:

(2) 需要编译.hs 文件。为此,您可以在提示后键入:

  • ghc hello.hsor
  • ghc --make hello
  • 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 可执行文件。