[Haskell-cafe] Wrapping a C++ library

Richard Lewis richard.lewis at gold.ac.uk
Mon Jan 26 15:45:08 UTC 2015

Hi there,

There is a C++ library for which I'm working on an FFI wrapper. I've
written a translation unit which defines several functions with an
extern "C" API. Each of the functions does a little bit of C++ to
operate the third party library, but the point is that I now have a C
API that I can wrap with the FFI.

The next thing I did was to try and get Cabal to compile this
translation unit for me. I eventually hacked together a Setup.hs that
I barely understand that calls gcc to compile the .cpp file and then
calls `ar` to, um, well do whatever it is that ar does with .o files,
make some sort of archive, or something.

Next, I need to get my hsc file (in which I define some imported
functions and some data types with Storable instances for shovelling
data through the FFI) to compile. This file uses the hsc2hs #include
macro to include the .h file of my translation unit. In turn, that .h
includes some of the C++ header files from the third party, and
eventually an STL header: vector. At this point, the compiler, gcc,
complains that it can't find vector.

The gcc command that hsc2hs (I think?) is creating does not include
-lstdc++, and nethier does it use g++, either of which would, I think,
result in a successful compilation of the hsc2hs .c file.

In my cabal file I have:

  extra-libraries: stdc++
  ghc-options: -pgml g++ -Wall

But these don't seem to fix anything. Perhaps they don't have any
effect on how hsc2hs operates?

There's a specific question here which goes something like: how do I
get hsc2hs to compile code that uses STL headers?

But there's also a more general question as to whether my whole
approach of creating an extern "C" wrapper is actually going to work?

Any answers would be very appreciated.


More information about the Haskell-Cafe mailing list