从Haskell调用C ++时运行`stack ghci`时遇到麻烦

时间:2016-10-19 20:34:46

标签: c++ haskell ghc ghci haskell-stack

我正在尝试做什么:我试图通过其FFI从Haskell调用C ++(通过extern "C" { ... }接口)。特别是,我有一个C ++文件three.cpp;在它内部是一个单独的外部C ++函数,我试图从Haskell访问(在后台使用其他一些私有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标志的,但是失败了。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

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

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

void foo();

#ifdef __cplusplus
}
#endif