GHCi从不加载编译的文件 [英] GHCi never loads compiled files
问题描述
写一个模块:
module Foo其中
foo = 3.14
$ c $
Foo.hs
加载它:
ghci -ignore-dot-ghci
GHCi,版本7.8.3:http://www.haskell.org/ghc/:?寻求帮助
加载包ghc-prim ...链接...完成。
加载包integer-gmp ...链接...完成。
加载程序包库...链接...完成。
前奏> :l Foo
[1的1]编译Foo(Foo.hs,解释)
好了,模块加载:Foo。
GHCi为什么不拿起编译好的代码?这是GHCi中的一个错误吗?
我尝试使用 -v
运行,不太有帮助。
更新
我发现如果模块使用 GHCi -fobject-code Foo
,即使没有 -fobject-code
,GHCi也会加载编译版本。
Update 2
这一切都发生在Linux机器上的Ghc 7.8上。 Ghc 7.6在Windows机器上不会出现这个问题。 解决方案
从 GHS 7.8.1发行说明
< blockquote>
在Linux,FreeBSD和Mac OS X上,GHCi现在默认使用系统动态
链接器,而不是内置的(静态)对象链接器。
这是更稳健的跨平台,并修复了许多长期存在的错误
(例如:构造函数和析构函数,弱符号等,正确运行
,并且RTS中的几个边界情况是)
因此,GHCi(和模板Haskell)现在必须加载动态的
目标文件,而不是静态的。为了解决这个问题,还有一个新的
编译标志,即动态编译标志,在编译期间使用
时,会导致GHC在相同的
时间发出静态和动态目标文件。 GHC本身仍默认为静态链接。
[...]
目前,Windows(32位或64位)不支持Dynamic GHCi和-dynamic。 $ b
最后一句解释了为什么这不会影响windows。
你可以看到问题所在比较 ghci -fobject-code Foo
和 ghc Foo
生成的文件:
$ mkdir ghci ghc
$ ghc Foo -odir ghc -hidir ghc
$ ghci -fobject-code Foo -odir ghci -hidir ghci< / dev / null
$ diff -u<(ghc --show-iface ghc / Foo.hi)<(ghc --show-iface ghci / Foo.hi)
--- / proc / self / fd / 11 2014-07-30 13:03:34.977845398 +0200
+++ / proc / self / fd / 12 2014-07-30 13:03:34.978845419 +0200
@@ -3,13 +3,13 @@
版本:通缉[7,0,8,3],
得到[7,0,8,3]
途径:通缉[],
- got []
+ got [d,y,n]
interface main:Foo 7083
interface hash:9aab457c4ecbd2507529fcb479523944
ABI hash:375e3124c60d5f4eb51e7e38e71a3be0
export-list hash:ff40b932e3d14f0fc26fbd56a58e227c
孤立hash:693e9af84d3dfcc71e640e005bdc5e2e
- flag hash:6c2d0082779adc2bd558d879f3f0fe26
+ flag hash:82fa613fe97939e6767d853897fe1074
使用TH拼接:False
其中
出口:
这表明GHCi将haskell文件编译为 dynamic 目标文件( dyn
方式),而
GHC编译为静态目标文件。
当GHCi尝试加载Foo时,它注意到Foo没有可用的动态目标文件(因为GHC生成了一个静态目标文件),因此它会重新编译Foo。如果给出了 -fobject-code
,那么现在将覆盖静态目标文件并生成一个动态文件
,以便下次GHCi不需要重新编译开始。如果 -fobject-code
不是指定的
,那么不会生成目标代码,并且该文件将以解释模式加载。
您还可以通过 ghc
来生成动态目标文件:
$ ghc -dynamic -c Foo
现在 ghci Foo
不重新编译Foo,但使用已编译的动态目标文件。
Write a module:
module Foo where
foo = 3.14
Compile it:
ghc -c Foo.hs
Load it:
ghci -ignore-dot-ghci
GHCi, version 7.8.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :l Foo
[1 of 1] Compiling Foo ( Foo.hs, interpreted )
Ok, modules loaded: Foo.
Why won't GHCi pick up compiled code? Is it a bug in GHCi?
I tried to run with -v
, not too helpful.
Update
I have found that If the module is compiled with GHCi -fobject-code Foo
, GHCi will subsequently load the compiled version, even without -fobject-code
.
Update 2
This all happens with Ghc 7.8 on a Linux machine. Ghc 7.6 on a Windows machine does not exhibit this problem.
From the GHC 7.8.1 Release Notes
On Linux, FreeBSD and Mac OS X, GHCi now uses the system dynamic linker by default, instead of its built in (static) object linker. This is more robust cross-platform, and fixes many long-standing bugs (for example: constructors and destructors, weak symbols, etc work correctly, and several edge cases in the RTS are fixed.)
As a result of this, GHCi (and Template Haskell) must now load dynamic object files, not static ones. To assist this, there is a new compilation flag, -dynamic-too, which when used during compilation causes GHC to emit both static and dynamic object files at the same time. GHC itself still defaults to static linking. [...]
Currently, Dynamic GHCi and -dynamic-too are not supported on Windows (32bit or 64bit.)
The last sentence explains why this doesn't affect windows.
You can see the problem by comparing the files that ghci -fobject-code Foo
and ghc Foo
generates:
$ mkdir ghci ghc
$ ghc Foo -odir ghc -hidir ghc
$ ghci -fobject-code Foo -odir ghci -hidir ghci < /dev/null
$ diff -u <(ghc --show-iface ghc/Foo.hi) <(ghc --show-iface ghci/Foo.hi)
--- /proc/self/fd/11 2014-07-30 13:03:34.977845398 +0200
+++ /proc/self/fd/12 2014-07-30 13:03:34.978845419 +0200
@@ -3,13 +3,13 @@
Version: Wanted [7, 0, 8, 3],
got [7, 0, 8, 3]
Way: Wanted [],
- got []
+ got [d, y, n]
interface main:Foo 7083
interface hash: 9aab457c4ecbd2507529fcb479523944
ABI hash: 375e3124c60d5f4eb51e7e38e71a3be0
export-list hash: ff40b932e3d14f0fc26fbd56a58e227c
orphan hash: 693e9af84d3dfcc71e640e005bdc5e2e
- flag hash: 6c2d0082779adc2bd558d879f3f0fe26
+ flag hash: 82fa613fe97939e6767d853897fe1074
used TH splices: False
where
exports:
This shows that GHCi compiles haskell files to dynamic object files (dyn
way), while
GHC compiles to static object files.
When GHCi tries to load Foo, it notices that there is no dynamic object file for Foo available (because GHC generated a static one), and thus it recompiles Foo. If -fobject-code
is given, this will now override the static object file and generate a dynamic one,
so that no recompilation is needed the next time GHCi is started. If -fobject-code
is not
specified, no object code is generated and the file is loaded in interpreted mode.
You can also tell ghc
to generate dynamic object files:
$ ghc -dynamic -c Foo
Now ghci Foo
doesn't recompile Foo, but uses the already-compiled dynamic object file.
这篇关于GHCi从不加载编译的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!