# cat Query.hs
main = putStrLn "Can ghc automatically run the executable?"
# ghci -fobject-code -e "main" Query.hs
Can ghc automatically run the executable?
# ls Query.*
Query.hi Query.hs Query.o
#
If you don’t want those extra files, use runghc:
# runghc --help
Usage: runghc [runghc flags] [GHC flags] module [program args]
The runghc flags are
-f /path/to/ghc Tell runghc where GHC is
--ghc-arg=... Pass an option or argument to GHC
--help Print this usage information
--version Print version number
#
Note that runghc doesn’t compile your programs. It runs programs using the interpreter (like GHCi), which is much slower at run time but faster at compile time.
That’s why there’s no extra files - they’re never generated to begin with. runghc is best used for small tasks and projects - larger ones are better served by @jaror’s or @hellwolf’s suggestions.