Statically link C++ library with a Haskell library

后端 未结 2 552
夕颜
夕颜 2020-12-15 09:31

Setup: I have a Haskell library HLib which makes calls to a C/C++ backend CLib for efficiency. The backend is small and specialized for use with

相关标签:
2条回答
  • 2020-12-15 09:51

    Including a C or C++ library with a Haskell library is trivial once you know a few tricks.

    I got the core from this article, though it seems to overcomplicate things. You can use cabal (currently 1.25) with a Simple build type (i.e. no special Setup.hs), no makefile, and no external tools like c2hs.

    To include symbols from a pure C library:

    1. In your cabal file, either add Include-dirs: relative/path/to/headers/ or Includes: relative/path/to/myheader.h.
    2. Add C-sources: relative/path/to/csources/c1.c, relative/path/to/csources/c2.c, etc.

    There's a couple of extra bits for C++:

    1. You can add .cpp files to the C-sources field in the cabal file.
    2. On all functions in .cpp files that Haskell needs access to, add extern "C" to avoid name mangling.
    3. Surround all non-pure-C code in header files with #ifdef __cplusplus ... #endif (see n.m.'s answer).
    4. If you use the standard C++ library, you'll need to add extra-libraries: stdc++ to your cabal file, and link with g++ using ghc-options: -pgmlg++.
    5. You may have to fiddle a bit with the order that you list the .c(pp) files in the cabal file if you want dynamic linking (i.e. cabal repl) to work. See this ticket for more information.

    That's it! You can see a complete working example here which works with both stack and cabal.

    0 讨论(0)
  • 2020-12-15 09:55

    GHC cannot really understand C++ header files. It needs pure C code. The common way for a C++ header file to provide C interface is to isolate away C++ parts with #ifdef __cplusplus, e.g.:

    #ifdef __cplusplus
    extern "C" {         // C compilers and various C-based FFIs don't like this
    #endif
    
    void foo();
    
    #ifdef __cplusplus
    }
    #endif
    

    In addition, GHCi is historically known to have problems with linking C++ code. For example, at one point it didn't understand weak symbols (often produced by the compiler in conjunction with inline functions and template instantiations). You might be seeing one of these problems. I'd recommend filing a bug report to the GHC team.

    0 讨论(0)
提交回复
热议问题