我正在尝试编写一个Haskell库,并在stack ghci中收到错误消息:
ghci> functionThatCalls_cSmithNormalForm 0
ghc-9.4.5: ^^ Could not load 'csmith_normal_form', dependency unresolved. See top entry above.
GHC.ByteCode.Linker: can't find label
During interactive linking, GHCi couldn't find the following symbol:
csmith_normal_form
每当我调用在SNF.hs中定义的函数 cSmithNormalForm 时:
foreign import ccall "csmith_normal_form" cSmithNormalForm :: Ptr CLLong -> IO (Ptr CLLong)
C++函数在snf.cpp文件中导出(整个项目中唯一的C++文件)如下:
using i64 = long long;
(...)
extern "C" {
i64 *csmith_normal_form(i64[]);
i64 *csmith_normal_form(i64 _mx[]) {
(...)
}
}
经过多次尝试创建此链接后,我的package.yaml文件包含以下内容:
cxx-sources:
- src/snf.cpp
cxx-options:
- -std=c++17
include-dirs:
- src
library:
source-dirs: src
cxx-sources:
- src/snf.cpp
cxx-options:
- -std=c++17
when:
- condition: os(linux)
extra-libraries: stdc++
stack.yaml文件保持不变。SNF.hs和snf.cpp都位于同一个目录(src)中。
尽管出现错误,但stack build
运行成功。
有没有办法修复错误并成功地从Haskell调用C++函数?此外,是否有关于如何使用像cxx-options这样的选项的可用文档?我找不到任何官方的资料。