[Haskell-cafe] Wrapping a C++ library

Richard Lewis richard.lewis at gold.ac.uk
Tue Jan 27 15:40:51 UTC 2015

Donn, Arjun, Alexey, Ian,

Thanks, all, for your suggestions.

At Mon, 26 Jan 2015 08:29:24 -0800,
Donn Cave wrote:
> Quoth Richard Lewis <richard.lewis at gold.ac.uk>,
> ...
> > There's a specific question here which goes something like: how do I
> > get hsc2hs to compile code that uses STL headers?
> For me, that would be the first thing to fix.  Use #ifdef __cplusplus,
> or just reorganize, so the hsc2hs-generated files are just C.
> You wrote your wrapper library for the C++ part, and if the callers
> need to be C++ it kind of defeats the purpose.

Yes, so it turns out the problem was that my wrapper .h was including
one of the headers of the third party library which, in turn, was
including vector. But I've managed to arrange it so that I don't need
to include that third party header anymore so gcc can compile my
translation unit without problems.

> From there, I would have guessed "extra-libraries: stdc++" would do it,
> (assuming you have also arranged to get your wrapper library in there),
> but I'm no cabal expert.  I use C++ more or less like you're doing, so 
> there's hope, but I haven't gotten around to the cabal part of the
> exercise.

The challenge with Cabal now is to get it to link my .a library with
the libHSMyLibrary.a that GHC creates. It seems, from what I've read
[1], that there is no Cabal hook into the linker so it may involve
quite a bit of work in the Setup.hs file. I was just following up a
suggestion [2] that (I think), if the .a file Cabal has created for my
wrapper static library is in the same directory as, um, not sure
what--output libHSMyLibrary.a?  gcc working directory?--and if its
name is of the form "libxxx.a", then it may be possible to add -lmylib
to gcc's options, and gcc will pick it up. Anyway, I altered my
Setup.hs to do this, putting libmylib.a in dist/build and dynamically
adding /path/to/dist/build to the "extra-libraries" option, but nm
still reports that the resulting libHSMyLibrary.a does not contain
libmylib.a (or would it be mylib.o?).

This might be a discussion for a new thread, however. The immediate
problem of how to compile the C wrapper is now fixed.


[1] http://stackoverflow.com/questions/18543228/statically-linking-a-c-library-with-a-haskell-library
[2] https://www.haskell.org/pipermail/haskell-cafe/2006-June/016022.html

More information about the Haskell-Cafe mailing list