linuxhaskelllinux-mint

I cannot execute file compiled by haskell compilier


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!"

Solution

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