How to link custom object file with Haskell library?

后端 未结 1 1735
你的背包
你的背包 2021-02-12 05:00

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 linke

相关标签:
1条回答
  • 2021-02-12 05:30

    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?

    0 讨论(0)
提交回复
热议问题