haskell - 如何在 Haskell 项目中使用 DLL?

标签 haskell shared-libraries ghc cabal haskell-stack

我想使用一个外部库,RDFox ,在 Haskell 项目中。

上下文:我正在使用 GHC 7.10 和 stack 处理 64 位的 Windows 和 Linux . RDFox 是用 C++ 编写的。可以使用 Java 和 Python 包装器下载 RDFox 共享库(.dll、.so)。

目标:我想在我的 Haskell 项目中重用 RDFox (.dll, .so) 编译的库,所以我需要为 RDFox 创建一个 Haskell 包装器。

问题:作为 Haskell 的新手,我很难知道从哪里开始。我找到了几个关于该主题的页面(来自 Haskell wiki 和 StackOverflow),但我并不清楚工作流程和配置。

问题:我想知道:

  1. How to configure stack and cabal to use external library, to build on Windows or Linux (different machines, same repository).
  2. How to configure GHCi for interactive testing on this external library.
  3. Is the translation of the Python wrapper to Haskell the best way to go? I would like to avoid the analysis of the RDFox C++ code.

最佳答案

  • 您需要使用 extra-lib-dirsextra-librariesexecutable您的 .cabal 的部分像这样的文件:
    name:                 MyApp
    version:              0.1.0.0
    synopsis:
    homepage:
    author:               simon.bourne
    category:
    build-type:           Simple
    cabal-version:        >=1.10
    
    library
      exposed-modules:      HelloWorld
      build-depends:        base >= 4.7 && < 5
      hs-source-dirs:       src
      default-language:     Haskell2010
    
    executable MyApp
      main-is:              Main.hs
      extra-lib-dirs:       lib
      extra-libraries:      helloWorld
      build-depends:        base >= 4.7 && < 5,
                            MyApp
      hs-source-dirs:       app
    
    default-language: Haskell2010
    

    将您的 dll 和 .so 放入 lib .请注意,如果您在 linux 上使用静态库( .a 而不是 .so ),则会遇到链接顺序问题。

    this例如。不要被这个名字所迷惑,因为它与 .so 配合得很好文件。
  • stack ghci只要它可以找到您的库(LD_LIBRARY_PATH 在 linux 上)就可以工作。
  • C API(在您的问题的评论中提到)已经存在。您只需要编写 Haskell FFI 签名,例如:
    foreign import ccall safe "helloWorld" c_helloWorld :: IO ()
    

    我强烈建议使用 safe ccalls,并且不要将函数包装在 unsafePerformIO 中.

    如果您需要传递非透明结构,您可能需要调查 c2hshsc2hs ,但我认为您不需要这样做。看到这个question更多细节。
  • 关于haskell - 如何在 Haskell 项目中使用 DLL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31201161/

    相关文章:

    haskell - Haskell的bind函数是什么类型的?

    haskell - 对大量数字求和太慢

    haskell - 值得阅读和学习的好 Haskell 资源

    android - cc_prebuilt_library_shared 模块无法识别的属性 "cflags"

    c - 在我用 C 语言编写的共享库中允许和不允许导出的函数 - 公共(public)和私有(private)库

    c++ - 如果链接到未使用的库,可执行文件的构建是否不同?

    Haskell 导入错误 : Not in scope

    haskell - 如何将 &&& 与 a -> 一起使用

    scala - 它如何是自然的转变?

    haskell - 如何使用 lambda 表达式将字符串转换为小写