如何阻止GHC生成中间文件?
当通过 ghc --make foo.hs 编译 haskell 源文件时,GHC 总是留下除 foo.exe 之外的各种中间文件。它们是 foo.hi
和 foo.o
。
我经常不得不删除 .hi 和 .o 文件以避免文件夹混乱。
GHC 是否有一个命令行选项可以不留下中间文件? (当在 #haskell 上询问时,我得到的最佳答案是 ghc --make foo.hs && rm foo.hi foo.o 。
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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我已经浏览了一些 GHC 文档,似乎没有一种内置方法可以自动删除临时文件——毕竟,GHC 需要这些中间文件来构建最终的可执行文件,并且它们的存在速度当 GHC 知道它不必重新编译模块时,进行整体编译。
但是,您可能会发现设置
-outputdir option
会帮助你;这会将所有目标文件 (
.o
)、接口文件 (.hi
) 和 FFI 存根文件放置在指定目录中。它仍然“混乱”,但至少它不再位于您的工作目录中。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.GHC 现在有选项
no-keep-hi -files
和no-keep-o-files
。 请参阅此处了解更多信息。GHC now has the options
no-keep-hi-files
andno-keep-o-files
. See here for more information.我通常的工作流程是直接使用 cabal 而不是 ghc。这会将outputdir选项设置到适当的构建文件夹中,并且可以为您执行诸如构建haddock文档之类的操作。您所需要做的就是为您的项目定义 .cabal 文件,然后说 cabal install 或 cabal build 而不是直接运行 ghc。由于如果您想分享您的 hackage 工作,最终需要遵循此过程,因此这是一个很好的实践,并且它也有助于管理包依赖项。
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.
我认为您可以将 -hidir 设置为 /dev/null,将它们发送到那里。此外,-fno-code 选项通常会关闭大量输出。您可能只想使用 Cabal。
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.
事实证明,使用 -hidir/-odir/-outputdir 是不好的; /dev/null 是一个文件,而不是一个目录。请参阅http://www.haskell.org/pipermail/xmonad/2010 -May/010182.html
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
2 美分即可稍微改进工作流程:
我们可以将以下别名放入
.bashrc
(或类似的)配置中文件:
然后调用
2 cents to improve the workflow a bit:
We can put the following alias into the
.bashrc
(or similar) configfile:
And then just call