简体   繁体   English

GHC(在Linux上)生成的.hi和.o文件是什么?

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

I am just getting started with Haskell and functional programming in general. 我刚刚开始使用Haskell和函数式编程。 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. 在编译并运行我的第一个Haskell程序之后,我注意到GHC(在Linux上)从单个.hs源文件生成三个单独的文件:.hi文件,.o文件,最后是可执行文件。 What is the purpose of each of these files? 每个文件的目的是什么? Practically, when are .hi and .o files actually used? 实际上,什么时候实际使用.hi和.o文件?

From my little experience in C, I think .o is an object file. 根据我在C方面的经验,我认为.o是一个目标文件。 Searching Google shows that .hi is an "interface file". 搜索Google显示.hi是一个“界面文件”。

The .o is exactly the same as C's object files; .o与C的目标文件完全相同; the .hi file is an "interface file"; .hi文件是一个“接口文件”; 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). 它包含GHC需要的.o信息,如果您编译其他模块,则能够链接该.o文件(所述信息不能存储在标准的.o文件中)。

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. 你可以说.hi文件相当于C的头文件(即扩展名为.h),只有GHC从原来的Haskell源生成。

Thus, the .hi is used when GHC compiles other modules, and the .o is used when linking all modules together to produce the executable. 因此,当GHC编译其他模块时使用.hi,并且在将所有模块链接在一起以产生可执行文件时使用.o。

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). 成功生成可执行文件后,您可以安全地删除.hi和.o文件(如果您想进行一些小的更改并快速重建,则可以保留它们 - 这将节省不必要的重新编译时间)。

.o files are indeed object files. .o文件确实是目标文件。 Basically chunks of bytecode ready to be linked together. 基本上字节码块准备链接在一起。

.hi files are interface files. .hi文件是接口文件。 The short version is that they hold something like compiled type signatures along with information that lets GHC perform optimization across file boundaries. 简短的版本是它们包含类似编译类型签名的信息以及允许GHC跨文件边界执行优化的信息。


I personally find having these files in my working directory annoying enough that I add -outputdir ../tmp to my ghc invocation. 我个人觉得在我的工作目录中有这些文件很烦人,我把-outputdir ../tmp添加到我的ghc调用中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM