Haskell动态库 [英] Haskell dynamic library
问题描述
http://www.vex.net/~trebla/haskell/so。 xhtml 介绍如何编译共享库。
http://www.vex.net/~trebla/haskell/so.xhtml describes how to compile shared library.
关于编译命令:
ghc -O2 -dynamic -shared -fPIC -o libEval.so Eval.hs hsbracket.c -lHSrts-ghc7.6.3
它说:
(你可以省略-dynamic来请求其他包的静态库吗?真的,它们不是用-fPIC生成的,特别是在x86_64上是非法的。)
(Could you omit -dynamic to request static libraries of other packages? Not really, they were not generated with -fPIC. In particular it is illegal on x86_64.)
为什么会这样?编译没有libHS *依赖的共享库应该怎么办?
Why is it so? What should one do to compile shared library without libHS* dependencies?
推荐答案
是的,用-fPIC编译有帮助。这是如何做到的。
Yes, compiling with -fPIC helps. Here is how to do that.
ghc-7.8.4 / mk / build.mk
:
SRC_HC_OPTS = -H64m -O
EXTRA_HC_OPTS = -fPIC
SRC_CC_OPTS = -fPIC -O
GhcStage1HcOpts = -fasm -O0
GhcStage2HcOpts = -fasm -O0
GhcLibHcOpts = -fasm -O2
GhcLibWays = v dyn
DYNAMIC_GHC_PROGRAMS = YES
DYNAMIC_BY_DEFAULT = NO
SplitObjs = NO
HADDOCK_DOCS = NO
BUILD_DOCBOOK_HTML = NO
BUILD_DOCBOOK_PS = NO
BUILD_DOCBOOK_PDF = NO
在编译ghc时:
export EXTRA_CONFIGURE_OPTS="--disable-library-profiling --enable-shared"
使用-fPIC构建cabal软件包:
To build cabal packages with -fPIC use:
cabal install --enable-shared --ghc-option=-fPIC text
测试文件 foo.hs
(Data.Text用于查看cabal包是否也正常工作):
Test file foo.hs
(Data.Text is used to see if cabal packages also work):
import Foreign.C as C
import Data.Text as T
import Data.Text.Foreign as T
foreign export ccall len :: CString -> IO CInt
len t = C.peekCString t >>= return . CInt . fromIntegral . T.length . T.pack
main = return ()
动态版本:
ghc -dynamic --make foo.hs
b $ b
动态与静态构建混合(不确定是否需要pthread,但它说明了如何添加动态链接):
Dynamic mixed with static build (not sure if pthread is needed but it illustrates how to add dynamic linking):
ghc -fPIC -shared --make -o libfoo.so \
-optl-Wl,-Bstatic -lHSrts -lCffi \
-lHSbase-4.7.0.2 -lHSinteger-gmp-0.5.1.0 -lHSghc-prim-0.3.1.0 \
-optl-Wl,-Bdynamic -lpthread foo.hs
这篇关于Haskell动态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!