我想使用一个外部库,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),但我并不清楚工作流程和配置。
问题:我想知道:
- How to configure stack and cabal to use external library, to build on Windows or Linux (different machines, same repository).
- How to configure GHCi for interactive testing on this external library.
- 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-dirs
和 extra-libraries
在 executable
您的 .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 上)就可以工作。 foreign import ccall safe "helloWorld" c_helloWorld :: IO ()
我强烈建议使用
safe
ccalls,并且不要将函数包装在 unsafePerformIO
中.如果您需要传递非透明结构,您可能需要调查
c2hs
或 hsc2hs
,但我认为您不需要这样做。看到这个question更多细节。 关于haskell - 如何在 Haskell 项目中使用 DLL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31201161/