Sudantha
Sudantha

Reputation: 16184

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 -> .exe

Upvotes: 20

Views: 26772

Answers (5)

MLev
MLev

Reputation: 435

On Windows (without using Cygwin) there are two steps to creating the .exe file from the .hs file:

(1) You need to navigate in the command line terminal to the folder containing your .hs file. 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
  • 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.

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

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

Either option will produce a hello.exe executable file in the same folder as your hello.hs file.

Upvotes: 0

Kirill G.
Kirill G.

Reputation: 960

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:

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.

Upvotes: 6

Don Stewart
Don Stewart

Reputation: 137937

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

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

Given a file:

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

Compile it with 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:

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

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

Upvotes: 33

Landei
Landei

Reputation: 54574

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

Upvotes: 1

mdm
mdm

Reputation: 12630

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

Upvotes: 3

Related Questions