tags:

views:

278

answers:

5

When compiling a haskell source file via ghc --make foo.hs GHC always leaves behind a variety of intermediate files other than foo.exe. These are foo.hi and foo.o.

I often end up having to delete the .hi and .o files to avoid cluttering up the folders.

Is there a command line option for GHC not to leave behind its intermediate files? (When asked on #haskell, the best answer I got was ghc --make foo.hs && rm foo.hi foo.o.

+5  A: 

I've gone through the GHC docs a bit, and there doesn't seem to be a built-in way to remove the temporary files automatically -- after all, GHC needs those intermediate files to build the final executable, and their presence speeds up overall compilation when GHC knows it doesn't have to recompile a module.

However, you might find that setting the -outputdir option will help you out; that will place all of your object files (.o), interface files (.hi), and FFI stub files in the specified directory. It's still "clutter," but at least it's not in your working directory anymore.

Mark Rushakoff
+4  A: 

My usual workflow is to use cabal rather than ghc directly. This sets the outputdir option into an appropriate build folder and can do things like build haddock documentation for you. All you need is to define the .cabal file for your project and then say cabal install or cabal build instead of run ghc directly. Since you need to follow this process in the end if you ever want to share your work on hackage, it is a good practice to get into and it helps manage package dependencies as well.

Edward Kmett
That's what I do as well. For every piece of Haskell code that's bigger than a single file, I setup a .cabal file. That is, I just copy an existing .cabal file from somewhere and modify it. I think someone is working on a `cabal --init` command to setup a default working space, which would be quite useful.
Tom Lokhorst
I'm fairly new to Haskell, but that's something I really like about cabal. cabal creates one directory (dist) and puts all output-files in that directory while building. It keeps other directories clean.
davidbe
+2  A: 

You can set the -hidir to /dev/null, I think, sending them there. Also, the -fno-code option in general turns off a lot of output. You might just want to use Cabal.

Don Stewart
+1  A: 

Turns out that using -hidir/-odir/-outputdir is no good; /dev/null is a file, and not a directory. See http://www.haskell.org/pipermail/xmonad/2010-May/010182.html

gwern
A: 

I've added a GHC feature request for such a flag: http://hackage.haskell.org/trac/ghc/ticket/4114

gwern