6

I've been working on a small file which I've been compiling and running as I go. My directory contains Log.hs and LogAnalysis.hs.

LogAnalysis.hs looks like this:

{-# OPTIONS_GHC -Wall #-}
module LogAnalysis where

import Log

parseMessage :: String -> LogMessage
--some code...

main :: IO ()
main = do
    putStrLn (show (parseMessage ("Some log message text")))

When I compile the LogAnalysis.hs with GHC I was getting an executable, along with some other binary files:

$ ll
Log.hi
Log.hs
Log.o
LogAnalysis     <-- this is the executable which has disappeared
LogAnalysis.hi
LogAnalysis.hs
LogAnalysis.o

I made some small changes and now when I run ghc LogAnalysis.hs I get only the .hi and .o files but no executable. The output is:

[1 of 2] Compiling Log              ( Log.hs, Log.o )
[2 of 2] Compiling LogAnalysis      ( LogAnalysis.hs, LogAnalysis.o )

I'm not even sure what I changed, but it wasn't anything major. Any idea what could be triggering this? Is there some way to force GHC to produce an executable?

Specs: GHC 8.8.3, macOS 10.15.5

Josh Friedlander
  • 10,870
  • 5
  • 35
  • 75
  • 1
    But there is an executable? – Willem Van Onsem Jun 28 '20 at 16:47
  • 1
    Please include the command line you're using. Also, what is the name of the `LogAnalysis.hs` module? – dfeuer Jun 28 '20 at 17:18
  • 4
    Old version of GHC requires a `—make` switch: `ghc —make LogAnalysis.hs`. In newer versions this became automatic if the provided file is a `Main` module. Since the default module header name the module as `Main`, I suspect your changes include adding a module header, and naming the module as something other than `Main`. – Ruifeng Xie Jun 28 '20 at 17:34
  • Amazing detective work @Krantz. thank you, that was it! Solution was, since my file began `module LogAnalysis where import Log`, to compile with `ghc -main-is LogAnalysis LogAnalysis.hs` – Josh Friedlander Jun 28 '20 at 19:01
  • @JoshFriedlander, you should add that as an answer. – dfeuer Jun 28 '20 at 20:07
  • @dfeuer OK, done. Thanks for your help! Also, I'd be grateful if you could let me know if my explanation of the problem was accurate. – Josh Friedlander Jun 29 '20 at 06:33

2 Answers2

12

Since I was declaring the file as a module not named Main, GHC by default doesn't create an executable. In order to compile this module into an executable, we can use GHC's main-is flag. (Thanks to Krantz in the comments and Willem Van Onsem's answer here for this.) So compiling with

ghc -main-is LogAnalysis LogAnalysis.hs

gives the output

[2 of 2] Compiling LogAnalysis      ( LogAnalysis.hs, LogAnalysis.o )
Linking LogAnalysis ...

So GHC has linked the executable LogAnalysis which is the desired result.

dfeuer
  • 48,079
  • 5
  • 63
  • 167
Josh Friedlander
  • 10,870
  • 5
  • 35
  • 75
1

There is an easier way to fix it, delete the second line:

module LogAnalysis where

I believe that when you define it as a module, ghc understands that it is just a module that will be imported in another file so it doesn't create an executable.

Pablo
  • 49
  • 1
  • 5