Search code examples
windowshaskellbuildexecabal

Stop command prompt from opening when running exe file built from haskell project


I have created a small application that uses the gloss library for a GUI. When typing "cabal run [cabal file]" the project is built and run, and only prompts the GUI. When I instead go and click on the .exe file that was generated from the build, the GUI opens but so does the command prompt. I want to stop the command prompt from opening and only have the window from gloss open.


Solution

  • By default on Windows GHC builds console programs, which open a Command Prompt terminal if one isn’t open already. If you want to build a GUI-only program, you can pass -mwindow to the linker by giving the -optl -mwindow flag to GHC. If you’re using Cabal, you would add this flag to the ghc-options field in the executable stanza.

    Beware that if you use this flag, your program will not have any stdin, stdout, or stderr file handles, so actions like putStrLn, print, and getLine will not work, since they’re equivalent to hPutStrLn stdout, hPrint stdout, and hGetLine stdin. If you want to do any logging or printing, you’ll need to open a handle yourself (e.g. a file, terminal, or socket with another program reading the other end); however, Debug.Trace will still work if you need temporary tracing for debugging.

    You can find more helpful reference information in the GHC User’s Guide §16. Running GHC on Win32 Systems.