【问题标题】:Real World Haskell, Chapter 5, SimpleJSON compile errorReal World Haskell,第 5 章,SimpleJSON 编译错误
【发布时间】:2015-02-11 20:31:34
【问题描述】:

根据another question,Real World Haskell 的部分内容现已过时。我只在第 5 章,但在将简单示例编译为可执行二进制文件时遇到问题。

给出了两个模块:

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 (truncate 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

module Main () where

import SimpleJSON

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

带有首先编译 SimpleJSON 对象文件的说明:

$ ghc -c SimpleJSON.hs

接着是链接可执行文件:

$ ghc -o simple Main.hs SimpleJSON.o

这会导致错误提示“m​​ain”未导出:

[2 of 2] Compiling Main             ( Main.hs, Main.o )

Main.hs:1:1:
    The main function `main' is not exported by module `Main'

但是,如果我将main 添加到导出列表或省略空的导出列表,我会在链接阶段看到很多多个定义错误:

Linking simple ...
SimpleJSON.o:(.data+0x0): multiple definition of `__stginit_SimpleJSON'
./SimpleJSON.o:(.data+0x0): first defined here
SimpleJSON.o:(.data+0x0): multiple definition of `SimpleJSON_getArray_closure'
./SimpleJSON.o:(.data+0x0): first defined here
....
(.text+0x2d40): multiple definition of `SimpleJSON_JObject_static_info'
./SimpleJSON.o:(.text+0x2d40): first defined here
SimpleJSON.o: In function `SimpleJSON_JArray_info':
(.text+0x2d80): multiple definition of `SimpleJSON_JArray_static_info'
./SimpleJSON.o:(.text+0x2d80): first defined here
collect2: error: ld returned 1 exit status

假设此错误是由文本显示的过时代码或过时 ghc 接口导致的,那么编译此 SimpleJSON 示例的正确方法是什么?

【问题讨论】:

    标签: haskell ghc


    【解决方案1】:

    这应该可行:

    ghc -o simple Main.hs SimpleJSON.hs
    

    甚至像这样的东西应该可以工作:

    ghc -c SimpleJSON.hs
    ghc -c Main.hs
    ghc -o simple Main.o SimpleJSON.o
    

    或者正如@chi 指出的那样,你可以使用这个:

    ghc --make SimpleJSON.hs Main.hs
    

    【讨论】:

    • 谢谢西比。这两个例子似乎都有效。虽然第二个有一个小问题,但编译 Main 依赖于 SimpleJSON 的接口文件。简单地交换订单对我来说就可以了。也感谢chi。 --make 看起来是 ghc 提供的一个相当强大的工具!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    相关资源
    最近更新 更多