将haskell解释器(提示)构建为动态库,可从C ++使用:缺少Interpreter.dyn_hi [英] Building a haskell interpreter (hint) as dynamic library, useable from C++: Missing Interpreter.dyn_hi
问题描述
我想创建一个haskell解释器,我可以从linux上使用C ++。
I want to create a haskell interpreter that I can use from C++ on linux.
我有一个文件FFIInterpreter.hs在haskell实现解释器,函数通过FFI到C ++。
I have a file FFIInterpreter.hs which implements the interpreter in haskell and exports the functions via FFI to C++.
module FFIInterpreter where
import Language.Haskell.Interpreter
import Data.IORef
import Foreign.StablePtr
import Foreign.C.Types
import Foreign.C.String
import Control.Monad
import Foreign.Marshal.Alloc
type Session = Interpreter ()
type Context = StablePtr (IORef Session)
foreign export ccall createContext :: CString -> IO Context
createContext :: CString -> IO Context
createContext name = join ((liftM doCreateContext) (peekCString name))
where
doCreateContext :: ModuleName -> IO Context
doCreateContext name
= do let session = newModule name
_ <- runInterpreter session
liftIO $ newStablePtr =<< newIORef session
newModule :: ModuleName -> Session
newModule name = loadModules [name] >> setTopLevelModules [name]
foreign export ccall freeContext :: Context -> IO ()
freeContext :: Context -> IO ()
freeContext = freeStablePtr
foreign export ccall runExpr :: Context -> CString -> IO CString
runExpr :: Context -> CString -> IO CString
runExpr env input = join ((liftM newCString) (join (((liftM liftM) doRunExpr) env (peekCString input))))
where
doRunExpr :: Context -> String -> IO String
doRunExpr env input
= do env_value <- deRefStablePtr env
tcs_value <- readIORef env_value
result <- runInterpreter (tcs_value >> eval input)
return $ either show id result
foreign export ccall freeString :: CString -> IO ()
freeString :: CString -> IO ()
freeString = Foreign.Marshal.Alloc.free
项目与ghc,一切工作正常。我使用以下命令:
When I compile the whole project with ghc, everything works fine. I use the following command:
ghc -no-hs-main FFIInterpreter.hs main.cpp -lstdc++
但是haskell模块只是C ++项目的一小部分,我不想让整个项目依赖ghc 。
But the haskell module is only a small piece of the C++ project and I don't want the whole project to depend on ghc.
所以我想使用ghc构建一个动态库,然后使用g ++将它链接到项目。
So I want to build a dynamic library with ghc and then link it to the project using g++.
$ ghc -shared -fPIC FFIInterpreter.hs module_init.c -lstdc++
[1 of 1] Compiling FFIInterpreter ( FFIInterpreter.hs, FFIInterpreter.o )
Linking a.out ...
/usr/bin/ld: /usr/lib/haskell-packages/ghc/lib/hint-0.3.3.2/ghc-7.0.3/libHShint-0.3.3.2.a(Interpreter.o): relocation R_X86_64_32S against `.data' can not be used when making a shared object; recompile with -fPIC
/usr/lib/haskell-packages/ghc/lib/hint-0.3.3.2/ghc-7.0.3/libHShint-0.3.3.2.a: could not read symbols: Bad value
collect2: ld gab 1 als Ende-Status zurück
所以我添加了-dynamic关键字,也不起作用:
So I added the -dynamic keyword, but that also doesn't work:
$ ghc -dynamic -shared -fPIC FFIInterpreter.hs librarymain.cpp -lstdc++
FFIInterpreter.hs:3:8:
Could not find module `Language.Haskell.Interpreter':
Perhaps you haven't installed the "dyn" libraries for package `hint-0.3.3.2'?
Use -v to see a list of the files searched for.
我在系统中搜索Interpreter.dyn_hi但没有找到它。有办法得到它吗?
我也尝试手动安装提示,但这也不提供Interpreter.dyn_hi文件。
I searched my system for Interpreter.dyn_hi but didn't find it. Is there a way to get it? I also tried to install hint manually, but this also doesn't deliver the Interpreter.dyn_hi file.
推荐答案
您必须使用 - enable-shared
标志(使用cabal-install)安装库(并且所有这些都取决于)以获取。 dyn_hi
和 .dyn_o
文件。您可以考虑在〜/ .cabal / config
文件中设置该选项。
You have to install the library (and all it depends on) with the --enable-shared
flag (using cabal-install) to get the .dyn_hi
and .dyn_o
files. You may consider setting that option in your ~/.cabal/config
file.
也许最简单的方法是取消注释〜/ .cabal / config
中的共享:XXX
行,将选项设置为 True
和
Perhaps the easiest way is to uncomment the shared: XXX
line in ~/.cabal/config
, set the option to True
and
cabal install --reinstall world
cabal install --reinstall world
为了安全起见,首先使用 - dry-run
选项来检测问题。如果 - dry-run
输出看起来合理,则继续并重新安装 - 这将需要一段时间。
For safety, run that with the --dry-run
option first to detect problems early. If the --dry-run
output looks reasonable, go ahead and reinstall - it will take a while, though.
这篇关于将haskell解释器(提示)构建为动态库,可从C ++使用:缺少Interpreter.dyn_hi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!