Haskell,链接两个文件时出错。

3
我是一名有用的助手,可以为您翻译文本。

我已经开始阅读《Real World Haskell》这本书,涉及到编程方面的内容,但我遇到了一个无法解决的问题。 我有两个文件。第一个文件 SimpleJSON.hs 包含以下代码:

module SimpleJSON
    (
        JValue(..)
      , getString
      , getInt
      , getDouble
      , getBool
      , getObject
      , getArray
      , isNull
    ) where

data JValue = JString String
            | JNumber Double
            | JBool Bool
            | JNull
            | JObject [(String, JValue)]
            | JArray [JValue]
              deriving (Eq, Ord, Show)

getString :: JValue -> Maybe String
getString (JString s) = Just s
getString _           = Nothing

getInt (JNumber n) = Just n
getInt _           = Nothing

getDouble (JNumber n) = Just n
getDouble _           = Nothing

getBool (JBool b) = Just b
getBool _         = Nothing

getObject (JObject o) = Just o
getObject _           = Nothing

getArray (JArray a) = Just a
getArray _          = Nothing

isNull v = v == JNull

我使用了 "ghc -c SimpleJSON.hs" 命令来获取目标文件。然后在我的 Main.hs 文件中进行操作。

module Main (main) where

import SimpleJSON

main = print (JObject [("foo", JNumber 1), ("bar", JBool False)])

我正在导入第二个文件,但当我运行“ghc -o simple Main.hs SimpleJSON.o”以获取.exe文件时,出现以下错误:
collect2.exe: error: ld returned 1 exit status
`gcc.exe' failed in phase `Linker'. (Exit code: 1)

感谢您的帮助。
1个回答

1
编译器应该通过 import SimpleJSON 语句识别出 Main 依赖于 SimpleJSON。这意味着当您编译 Main.hs 时,SimpleJSON.hs 也会被编译并链接到生成的可执行文件中。
通过在命令行上明确指定 SimpleJSON.o,您可能会将该文件链接两次到生成的可执行文件中,这会导致您看到的失败。 ghc -o simple Main.hs 应该足以使您的程序链接。

1
不客气。如果您认为这是正确的答案,请通过点击复选框接受它。 - Dave Compton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接