Data.Aseon with ForeignFunctionInterface in ghc

I have the following haskell code:

{-# LANGUAGE OverloadedStrings, DeriveGeneric, DeriveAnyClass  #-}

module BoardToJSON where

import GHC.Generics
import Data.Aeson
import Data.Aeson.Text (encodeToLazyText)
import Data.Text
import Control.Applicative
import Control.Monad
import qualified Data.ByteString.Lazy as B
import GHC.Generics
import Data.Text.Lazy.IO as I

import Foreign.C.Types
import Foreign.C.String
import Data.Maybe
import Data.List
import Data.Ix

import Data.List
import Data.Maybe
--foreign export ccall writef :: IO ()

data Piece = Piece {
  _type :: !Text,
  _color :: !Text,
  _x :: Int,
  _y :: Int
  } deriving (Eq, Show, Generic, ToJSON, FromJSON)

piecesList::[Piece]
piecesList = [Piece "Rook" "White" 1 1, Piece "Knight" "White" 2 1, Piece "Bishop" "White" 3 1, Piece "Queen" "White" 4 1,
          Piece "King" "White" 5 1, Piece "Bishop" "White" 6 1, Piece "Knight" "White" 7 1, Piece "Rook" "White" 8 1,
          Piece "Pawn" "White" 1 2, Piece "Pawn" "White" 2 2, Piece "Pawn" "White" 3 2, Piece "Pawn" "White" 4 2,
          Piece "Pawn" "White" 5 2, Piece "Pawn" "White" 6 2, Piece "Pawn" "White" 7 2, Piece "Pawn" "White" 8 2,
          Piece "Rook" "Black" 1 8, Piece "Knight" "Black" 2 8, Piece "Bishop" "Black" 3 8, Piece "Queen" "Black" 4 8,
          Piece "King" "Black" 5 8, Piece "Bishop" "Black" 6 8, Piece "Knight" "Black" 7 8, Piece "Rook" "Black" 8 8,
          Piece "Pawn" "Black" 1 7, Piece "Pawn" "Black" 2 7, Piece "Pawn" "Black" 3 7, Piece "Pawn" "Black" 4 7,
          Piece "Pawn" "Black" 5 7, Piece "Pawn" "Black" 6 7, Piece "Pawn" "Black" 7 7, Piece "Pawn" "Black" 8 7]

jsonFile :: FilePath
jsonFile = "pieces.json"

writef = I.writeFile jsonFile (encodeToLazyText piecesList)

getJSON :: IO B.ByteString
getJSON = B.readFile jsonFile

getPieces :: IO (Either String [Piece])
getPieces = (eitherDecode <$> getJSON) :: IO (Either String [Piece])

      

and aeson

installed with cabal.

By doing:

$ ghci
Prelude> :load BoardToJSON
*BoardToJSON> writef

      

I am writing a Json file with an array of pieces in it.

However, when commenting out foreign export ccall writef :: IO ()

and compiling with:

CPP_SOURCES = main.cpp
HASKELL_SOURCES = haskell/BoardToJSON.hs
OBJECTS = haskell/*.o *.o

main: compileHaskell compileCPP link clean;

compileHaskell: $(HASKELL_SOURCES); ghc -c -XForeignFunctionInterface -fforce-recomp -O $(HASKELL_SOURCES)

compileCPP: $(CPP_SOURCES); g++ -c -I/usr/lib/ghc/include  -O $(CPP_SOURCES)

link: ; ghc -o Main -no-hs-main $(OBJECTS) -lstdc++

clean: ; rm -rf main && rm -rf haskell/*.o && rm *.o && rm -rf haskell/*.hi && rm -rf haskell/*_stub.h

      

compileHaskell

and it compileCPP

goes fine, but I get a bunch of errors like:

/tmp/ghca1ca_0/ghc_8.o:(.data.rel.ro+0x2a8): undefined reference to `bytestringzm0zi10zi8zi1_DataziByteStringziLazzy_getContents2_closure'
collect2: error: ld returned 1 exit status
`gcc' failed in phase `Linker'. (Exit code: 1)
makefile:12: recipe for target 'link' failed

      

I assume that ghc doesn't know where to find it aeson

, so I did:

$ sudo echo "/root/.cabal/lib/x86_64-linux-ghc-8.0.2" > /etc/ld.so.conf.d/x86_64-linux-gnu.conf
$ sudo ldconfig
$ sudo ldconfig -v | grep -i aeson                 
ldconfig: Path `/lib/x86_64-linux-gnu' given more than once
ldconfig: Path `/usr/lib/x86_64-linux-gnu' given more than once
ldconfig: /lib/x86_64-linux-gnu/ld-2.24.so is the dynamic linker, ignoring

libHSaeson-1.1.1.0-4sfmSwjYSZ4CJzSxs6L5hG-ghc8.0.1.so -> libHSaeson-1.1.1.0-4sfmSwjYSZ4CJzSxs6L5hG-ghc8.0.1.so

      

No chance.

I tried to add -llibHSaeson

, -lHSaeson

or -laeson

, to link the goal. But nothing happened.

Any idea?


Edit

After further research, I tried to reinstall aeson with --enable-shared

$ cabal install aeson --enable-shared --reinstall

      

Same problem.

+3


source to share


1 answer


When you provide files .o

as contributions to GHC, it doesn't know if they came from Haskell modules or not. As a result, it cannot know which Haskell links to bind.



You can specify dependencies manually on the command line of a pipe (for example -package aeson

), or provide the source Haskell modules on the communication command line, in which case GHC will automatically detect the dependencies.

+1


source







All Articles