Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GHC compiles to .o and .hi but no executable

Tags:

haskell

ghc

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

like image 376
Josh Friedlander Avatar asked Mar 03 '23 07:03

Josh Friedlander


1 Answers

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.

like image 153
Josh Friedlander Avatar answered Mar 11 '23 06:03

Josh Friedlander