I am just getting started with Haskell and functional programming in general. After compiling and running my first Haskell program, I noticed that GHC (on Linux) generates three separate files from a single .hs source file: a .hi file, a .o file, and finally an executable. What is the purpose of each of these files? Practically, when are .hi and .o files actually used?
From my little experience in C, I think .o is an object file. Searching Google shows that .hi is an "interface file".
The .o
is exactly the same as C's object files; the .hi
file is an "interface file"; it contains information about the .o
that GHC would need, if you compile other modules, to be able to link against that .o
file (said information cannot be stored in a standard .o
file).
You could say that the .hi
file is the equivalent of C's header files (that is, with .h
extension), only these are generated by GHC from the original Haskell source.
Thus, the .hi
is used when GHC compiles other modules, and the .o
is used when linking all modules together to produce the executable.
You can safely delete the .hi
and .o
files once you have successfully generated the executable (or keep them if you want to make some small change and rebuild quickly - it will save time in unneeded recompilations).
.o
files are indeed object files. Basically chunks of bytecode ready to be linked together.
.hi
files are interface files. The short version is that they hold something like compiled type signatures along with information that lets GHC perform optimization across file boundaries.
I personally find having these files in my working directory annoying enough that I add -outputdir ../tmp
to my ghc
invocation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With