Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the .hi and .o files generated by GHC (on Linux)?

Tags:

haskell

ghc

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".

like image 862
jmpgunn Avatar asked Jun 01 '19 04:06

jmpgunn


2 Answers

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).

like image 144
typedfern Avatar answered Oct 21 '22 01:10

typedfern


.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.

like image 35
John F. Miller Avatar answered Oct 21 '22 00:10

John F. Miller