在从Haskell调用C++时运行`stack ghci`出现问题

3
我所要做的事情: 我试图通过其FFI从Haskell调用C++(通过extern "C"{...}接口)。特别是,我有一个名为three.cpp的C++文件;它里面有一个被外部引用的C ++函数,我正在尝试从Haskell中访问它(其中一些其他私有C ++代码在后台运行)。 问题: 我可以成功运行stack build stack 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个回答

1
问题在于所有非纯C代码(即严格的C++代码)必须用#ifdef括起来:
#ifdef __cplusplus //I wasn't surrounding pure C++ code with these headers!
extern "C" {         
#endif

void foo();

#ifdef __cplusplus
}
#endif

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接