如何将自定义对象文件与Haskell库链接? [英] How to link custom object file with Haskell library?

查看:158
本文介绍了如何将自定义对象文件与Haskell库链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Haskell包,它可以对在CUDA代码中定义的函数进行FFI调用。我希望在包构建过程中将.cu文件编译为对象(.o)文件,并强制链接器将其链接到它。



到目前为止,我尝试使用一项技术发现这个问题。我定制了buildHook:


  1. 运行nvcc

  2. 运行default buildHook

  3. 使用nvcc编译代码创建ar库文件。

Setup.hs可用这里



这个解决方案在限制这个软件包方面有一个主要缺点静态链接。虽然cabal生成一个共享库,但它不会工作,因为它无法解析位于目标文件中的符号。



有没有更简单的方法来链接自定义代码在建设?

解决方案

我做类似的事情。我有一个调用CUDA代码的Haskell文件。



以下是我如何编译CUDA库并使用Haskell链接:



<$ $ NVCC)-c -E $(NVCC_OPTS)-o build / file.i file.cu
$(NVCC)-c $(NVCC_OPTS)-o build / file.o file.cu

然后将所有内容链接到名为 LibSO with Haskell options

  $(CXX)-shared -Wl,-rpath = \ $$$$ ORIGIN $(CXX_LINK_LIBS)$(PACKAGE_RPATH)-Lbuild -rdynamic -L / usr / local / lib / ghc-7.6.3 -lHSrts-ghc7.6.3 -o build / LibSO.so build / file.o 

其中

  CXX_LINK_LIBS = -Lbuild -lcudart -lcuda -lpthread -lcupti -lcurand -lnvidia -ml 
NVCC_OPTS = --compiler-options -fPIC -maxrregcount = 0 --machine 64 --DCUDA

然后将我的Haskell文件编译成o和hi文件。 (由于TemplateHaskell,我编译了两次)

$ $ $ $ $ $ $ ghc -v0 -Wall -rtsopts -threaded -stubdir build -ibuild / -no- hs-main -o build / iop.o -ohi build / iop.hi -c haskell / iop.lhs
ghc -v0 -Wall -rtsopts -threaded -stubdir build -ibuild / -no-hs-main - fPIC -dynamic -osuf dyn_o -hisuf dyn_hi -o build / iop.dyn_o -ohi build / iop.dyn_hi -c haskell / iop.lhs

现在我们有了haskell动态对象和一个C ++共享库。
最后,我将一个主要的haskell文件链接到一切: = \ $$ ORIGIN$(CXX_LINK_LIBS)-Lbuild -rtsopts -threaded -lstdc ++ -lLibSO -o build / Main build / iop.dyn_o

有这种帮助吗?


I've created a Haskell package that makes FFI calls to functions defined in CUDA code. I'd like to compile .cu file to an object (.o) file during package build and force linker to link it in.

So far, I tried to use a technique found this question. I've customized buildHook to:

  1. run nvcc
  2. run default buildHook
  3. create ar library file with nvcc compiled code.

Setup.hs is available here.

This solution has a major disadvantage in restricting this package to static linking. Although cabal produces a shared library, it won't work because it has no way of resolving symbols located in the object file.

Is there a simpler way to link custom code during building?

解决方案

I do a similar thing. I have a Haskell file which calls CUDA code.

Here's how I compile CUDA libraries and link with Haskell:

$(NVCC) -c -E  $(NVCC_OPTS) -o build/file.i file.cu
$(NVCC) -c  $(NVCC_OPTS) -o build/file.o file.cu

I then link everything into a C++ Shared Library called LibSO with Haskell options

$(CXX) -shared -Wl,-rpath=\$$$$ORIGIN $(CXX_LINK_LIBS) $(PACKAGE_RPATH) -Lbuild -rdynamic -L/usr/local/lib/ghc-7.6.3 -lHSrts-ghc7.6.3 -o build/LibSO.so build/file.o

where

CXX_LINK_LIBS = -Lbuild -lcudart -lcuda -lpthread -lcupti -lcurand -lnvidia-ml
NVCC_OPTS = --compiler-options -fPIC -maxrregcount=0 --machine 64 --DCUDA

I then take my Haskell files and compile them into o and hi files. (I compile twice because of TemplateHaskell)

ghc -v0 -Wall -rtsopts -threaded -stubdir build -ibuild/ -no-hs-main  -o build/iop.o -ohi build/iop.hi -c haskell/iop.lhs
ghc -v0 -Wall -rtsopts -threaded -stubdir build -ibuild/ -no-hs-main  -fPIC -dynamic -osuf dyn_o -hisuf dyn_hi -o build/iop.dyn_o -ohi build/iop.dyn_hi -c haskell/iop.lhs

So now we have haskell dynamic objects and a C++ shared library. In the end, I link a main haskell file with everything:

ghc -optl "-Wl,-rpath=\$$ORIGIN" $(CXX_LINK_LIBS) -Lbuild -rtsopts -threaded -lstdc++ -lLibSO -o build/Main build/iop.dyn_o

Does this sort of help?

这篇关于如何将自定义对象文件与Haskell库链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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