2016-10-19 13 views
3

무엇을하려고합니까? 하스켈에서 FFI를 통해 C++ (extern "C" { ... } 인터페이스 사용)을 호출하려고합니다. 특히 C++ 파일 three.cpp이 있습니다. 그 안에는 Haskell에서 액세스하려고하는 하나의 외부 C++ 함수가 있습니다 (백그라운드에서 다른 개인 C++ 코드 사용).Haskell에서 C++을 호출 할 때 'stack ghci`를 실행하는 데 문제가 발생했습니다.

문제점 :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

문제는 ALL이 아닌 순수 C 코드 (즉, 엄격한 C++ 코드)를 위해서 #ifdefs로 묶어야합니다 밝혀 :

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

void foo(); 

#ifdef __cplusplus 
} 
#endif