GHC动态并发仅适用于第一次编译

GHC动态并发仅适用于第一次编译

本文介绍了Haskell GHC动态并发仅适用于第一次编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照GHC教程,和更改此代码遵循,我创建了一个程序,它能够编译和运行一个模块在Test.hs与一个函数print打印一个字符串到屏幕:

Following the GHC tutorial posted here and alterations to this code following the advice in a previous stack overflow question I asked, I have created a program which is able to compile and run a module in Test.hs with a function print to print a string to the screen:

import GHC
import GHC.Paths
import DynFlags
import Unsafe.Coerce

main :: IO ()
main =
    defaultErrorHandler defaultLogAction $ do
      func <- runGhc (Just libdir) $ do
        dflags <- getSessionDynFlags
        setSessionDynFlags dflags
        target <- guessTarget "Test.hs" Nothing
        addTarget target
        r <- load LoadAllTargets
        case r of
          Failed -> error "Compilation failed"
          Succeeded -> do
            m <- findModule (mkModuleName "Test") Nothing
            setContext [IIModule m]
            value <- compileExpr ("Test.print")
            do let value' = (unsafeCoerce value) :: String -> IO ()
               return value'
      func "Hello"
      return ()


$ b b

这个代码的问题,如评论中指出的,它只是似乎工作的第一次运行它(当Test.hs还没有被遵守)。如果您尝试再次运行代码,则会出现以下错误:

The problem with this code, as noted in the comments, is that it only seems to work the first time you run it (When Test.hs has not yet been complied). If you attempt to run the code a second time, the following error appears:

mkTopLevEnv: not interpreted main:Test

我相信这与代码已经编译的事实有关。如果我删除.hi和.o文件并再次运行程序,程序将正确运行正确的输出。我缺少什么?我目前正在使用ghc版本7.4.1

I believe this has something to do with the fact that the code has already been compiled. If I delete the .hi and .o files and run the program again, the program runs correctly with the correct output. What am I missing? I am currently using ghc version 7.4.1

(注意:我试过查看GHC API,但找不到对mkTopLevEnv的任何引用)

(Note: I have tried looking through the GHC API but could not find any references to mkTopLevEnv)

推荐答案

Simon Marlow建议替换

Simon Marlow suggests here that replacing

guessTarget "Test.hs" Nothing

guessTarget "*Test.hs" Nothing

应该避免你得到的错误它告诉GHC不要加载.o文件。

should avoid the error you're getting, on the grounds that it tells GHC not to load the .o file.

请参阅

See the whole thread on a page via nabble

当然,你可以每次删除.hi和.o文件,但这是一个丑陋的解决方法。

Of course, you could delete the .hi and .o files each time, but that's an ugly workaround.

这篇关于Haskell GHC动态并发仅适用于第一次编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!