根据another question的说法,现实世界的一部分现在已经过时。我只讨论了第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这将导致一个错误,说明“main”未导出:
[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示例的正确方法是什么?
发布于 2015-02-11 20:35:42
这应该是可行的:
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.hshttps://stackoverflow.com/questions/28463916
复制相似问题