Haskell 动态库 [英] Haskell dynamic library

查看:25
本文介绍了Haskell 动态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://www.vex.net/~trebla/haskell/so.xhtml描述如何编译共享库.

关于编译命令:

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

动态混合静态构建(不确定是否需要 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆