我正在尝试创建一个Haskell库,该库旨在从其他语言中调用,尤其是C语言。
例如:
{-# LANGUAGE CPP #-}
{-# LANGUAGE ForeignFunctionInterface #-}
module Example where
import Foreign.C.String as CString
data Person = Person { firstName :: CString
, lastName :: CString }
foreign export ccall entrypoint :: IO (Person)
entrypoint :: IO (Person)
entrypoint = do
first <- newCString "John"
last <- newCString "Doe"
return (Person {firstName = first, lastName = last})
当我尝试编译时:
$ hsc2hs x.hsc && ghc x.hs
[1 of 1] Compiling Example ( x.hs, x.o )
x.hsc:11:1:
Unacceptable result type in foreign declaration:
‘Person’ cannot be marshalled in a foreign call
When checking declaration:
foreign export ccall "entrypoint" entrypoint :: IO (Person)
我该如何做到这一点,以及如何用尽可能少的样板做到这一点?
我也希望看到C结构自动生成,类似于:
struct Person {
char *firstName;
char *lastName;
};
也许我对语言和语言要求太多了工具,但在这种情况下,我真的很想知道(最佳)选项是什么。
谢谢!