1

I want to run this program as a standalone program:

module Main where main = putStrLn "Hello world!"

so I typed in ghci: --make -o hello Main.hs

but when I want to run it with ./hello I get a parse error for ./. I tried also ghc --make -o hello Main.hs but then I get an "Variable not in scope: ghc" error. Can somebody tell me, what I'm doing wrong?

Solved: the command has to be run in the shell and ghc and Main.hs has to be in the same directory

Temeraire
  • 11
  • 2

1 Answers1

2

Short answer: You do not compile the program in the ghci shell.

so I typed in ghci: --make -o hello Main.hs

It looks like you are running this in ghci (based on the error message Variable not in scope). ghci is a Haskell shell, but you do not write commands to compile or run a program in the Haskell shell itself. You run these in a shell like bash, sh, fish, etc.

So you open a terminal window, and then you can cd to the correct directory where the Main.hs file is located, then you can write:

ghc --make -o hello Main.hs

or for a Windows system, you can add a .exe extension:

ghc --make -o hello.exe Main.hs

or as @chi says, you can work with Haskell-stack:

stack ghc -- Main

Perhaps you need to add the directory of the executables to the PATH environment variable, see for example this question.

and finally you can run this program, for example by writing ./hello in the shell.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • If I try this, I get an error message which says, that the command "ghc" couldn't be found – Temeraire May 17 '20 at 22:30
  • 1
    @Temeraire: then it looks like you did not install `ghc` properly. Where is the binary of ghci located? Normally this is located under `/usr/bin/ghc` on a Debian system. – Willem Van Onsem May 17 '20 at 22:31
  • I have a windows system, it is located in C:\User\stack\x86_64-windows\ghc-8.8.3\bin – Temeraire May 17 '20 at 22:34
  • @Temeraire: and does that directory contain a `ghc` program? – Willem Van Onsem May 17 '20 at 22:35
  • 1
    @Temeraire: you can normally use `C:\User\stack\x86_64-windows\ghc-8.8.3\bin\ghc --make -o hello.exe Main.hs` I think to execute a program in another directory. – Willem Van Onsem May 17 '20 at 22:37
  • 1
    `stack ghc -- Main` should produce `Main.exe` on windows, assuming stack works. – chi May 17 '20 at 22:59
  • You *can* use GHCi as a shell, using something like turtle. GHCi also has a built-in command for running programs; I don't remember what it's called.. – dfeuer May 18 '20 at 00:42