簡體   English   中英

從Haskell調用C ++時運行`stack ghci`時遇到麻煩

[英]Trouble running `stack ghci` when calling C++ from Haskell

我正在嘗試做什么:我試圖通過其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標志的,但是失敗了。 有什么建議么?

問題是所有非純C代碼(即嚴格的C ++代碼)必須被#ifdefs包圍:

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

void foo();

#ifdef __cplusplus
}
#endif

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM