2

According to another question, parts of Real World Haskell are now obsolete. I'm only on Chapter 5, but I'm having problems compiling a simple example to an executable binary.

Two modules are given:

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

and

module Main () where

import SimpleJSON

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

with instructions to compile a SimpleJSON object file first:

$ ghc -c SimpleJSON.hs

followed by linking the executable:

$ ghc -o simple Main.hs SimpleJSON.o

which results in an error stating that 'main' is not exported:

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

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

However, if I add main to the export list or omit the empty export list, I see lots of multiple definition errors during the linking phase:

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

Assuming this error is the result of obsolete code or obsolete ghc interface presented by the text, what would be the proper way to compile this SimpleJSON example?

Community
  • 1
  • 1
bpaterni
  • 251
  • 1
  • 7

1 Answers1

5

This should work:

ghc -o simple Main.hs SimpleJSON.hs

Or even something like this should work:

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

Or as @chi points out, you can use this:

ghc --make SimpleJSON.hs Main.hs
Sibi
  • 43,989
  • 14
  • 80
  • 146
  • Thanks Sibi. Both examples seem to work. Though there is one small issue with the second, in that compiling Main depends on SimpleJSON's interface file. Simply swapping the order does the trick for me. Thanks to chi as well. `--make` looks to be a pretty powerful tool provided by ghc! – bpaterni Feb 11 '15 at 23:03