gpt4 book ai didi

c++ - 从 Haskell 调用 C++ 时无法运行 `stack ghci`

转载 作者:太空狗 更新时间:2023-10-29 22:58:30 26 4
gpt4 key购买 nike

我正在尝试做的事情:我正在尝试通过其 FFI 从 Haskell 调用 C++(通过 extern "C"{ ... } 接口(interface)) .特别是,我有一个 C++ 文件 three.cpp;它里面是我试图从 Haskell 访问的单个外部 C++ 函数(后台有一些其他私有(private) C++ 代码)。

问题:我可以成功运行stack buildstack test,一切都按预期进行。但是,当我运行 stack ghci 时,出现以下错误:

/usr/bin/ld: /home//Dropbox/Sling/.stack-work/dist/x86_64-linux/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/src/three.o: relocation R_X86_64_32 against `.bss' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
`gcc' failed in phase `Linker'. (Exit code: 1)

可能需要的信息:我项目的 .cabal 文件的相关部分如下所示:

executable Sling-exe
hs-source-dirs: app
main-is: Main.hs
ghc-options: -fPIC -threaded -rtsopts -with-rtsopts=-N
cc-options: -fPIC
extra-libraries: stdc++
build-depends: base
C-sources: src/three.cpp
Include-dirs: include
Includes: include/three.h
Install-includes: three.h
default-language: Haskell2010

请注意我是如何尝试包含 -fPIC 标志的,但失败了。有什么建议吗?

最佳答案

问题证明,所有 非纯 C 代码(即严格的 C++ 代码)必须被#ifdefs 包围:

#ifdef __cplusplus //I wasn't surrounding pure C++ code with these headers!
extern "C" {
#endif

void foo();

#ifdef __cplusplus
}
#endif

关于c++ - 从 Haskell 调用 C++ 时无法运行 `stack ghci`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40141042/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com