Data.Aeson 在 ghc 中使用 ForeignFunctionInterface

Data.Aeson with ForeignFunctionInterface in ghc

我有以下 haskell 代码:

{-# 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])

aeson 安装了 cabal。

做的时候:

$ ghci
Prelude> :load BoardToJSON
*BoardToJSON> writef

我正在编写一个 Json 文件,其中包含 pieces 数组。

但是,当取消注释 foreign export ccall writef :: IO () 并编译时:

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

compileHaskellcompileCPP 运行正常,但我收到一堆错误类型:

/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

我的猜测是 ghc 不知道在哪里可以找到 aeson 所以我找到了:

$ 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

没有机会。

我试图将 -llibHSaeson-lHSaeson-laeson 添加到 link 目标。但没有任何效果。

有什么想法吗?


编辑

经过进一步研究,我尝试用 --enable-shared

重新安装 aeson
$ cabal install aeson --enable-shared --reinstall

同样的问题。

当您提供 .o 文件作为 GHC 的输入时,它不知道它们是否来自 Haskell 模块。因此,它无法知道需要 link 编辑哪些 Haskell 依赖项。

您可以在 link 命令行中手动指定依赖项(例如 -package aeson),或者在 link 命令行中提供原始的 Haskell 模块在哪种情况下 GHC 将自动确定依赖关系。