I cannot execute file compiled by haskell compilier

82 Views Asked by At

I want to execute a binary file, which was compiled by the GHC Haskell compiler.

If I execute ./main.hi I get:

bash: ./main.hi: cannot execute binary file: Exec format error

and if I write ./main I get

bash: ./main: No such file or directory but file exists.

Result of the ls command:

log.txt  main.hi  main.hs  main.o

chmod didn't help to fix the problem. My Haskell code:

module HelloWorld where

main :: IO ()
main = putStrLn "Hello, World!"
1

There are 1 best solutions below

2
Daniel Wagner On BEST ANSWER

main must appear in a module named Main -- or, with GHC, you may (must) inform it of your alternative main module's name. So, you can either change your file to

module Main where

main :: IO ()
main = putStrLn "Hello, World!"

or change your compilation line to include the -main-is argument; for example:

% ghc -main-is HelloWorld main.hs

After making either of these changes, you should see an additional file in the list:

% ls
log.txt  main  main.hi  main.hs  main.o

This new file is the executable, and it can be run with ./main.