<div dir="ltr">Hi Arjun,<div><br></div><div>I've been doing quite a bit of this kind of thing recently, but it took me a while to figure it out too.  I've made a little example at <a href="https://github.com/ian-ross/cpp-ffi-example">https://github.com/ian-ross/cpp-ffi-example</a> -- just clone the repo and follow the instructions in the README.</div><div><br></div><div>The example has a C++ test library to build that doesn't do anything very interesting but provides some things to bind to.  It uses a C wrapper around the C++ library and C2HS to write the Haskell FFI side of things (you can almost certainly use hsc2hs in the same kind of way).  There's also a small test program so you see the bindings working end-to-end.  I guess the most critical thing is using extern "C" and the __cplusplus compiler macro to control the visibility of C++ code to GCC -- take a look at cbits/a.h and cbits/a.cpp in the repo to see what I mean.</div><div><br></div><div>Feel free to ask if you have any questions.</div><div><br></div><div>Cheers,</div><div><br></div><div>Ian.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 26 January 2015 at 23:55, Arjun Comar <span dir="ltr"><<a href="mailto:nrujac@gmail.com" target="_blank">nrujac@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Alexey,<br>It's unfortunately been about a year since I've worked on the project that needed FFI support so I don't recall the precise issue any longer. From talking to people in #haskell, I discovered that it would be necessary to call g++ and not gcc because gcc is unable to locate some headers and libraries that inevitably get included by C++ projects.<br><br>Thanks,<br>Arjun<br></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 26, 2015 at 5:49 PM, Alexey Shmalko <span dir="ltr"><<a href="mailto:rasen.dubi@gmail.com" target="_blank">rasen.dubi@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Arjun,<div><br></div><div>I'm not an expert in writing Haskell binding but pretty much experienced in C/low-level stuff.</div><div><br></div><div>You shouldn't force compilation of .c files with g++. Instead, you should keep your wrapping functions as extern "C" and place their implementation in .cpp file (which of course should be compiled with g++). Don't forget to link to C++ runtime library (-lstdc++).</div><div><br></div><div>There is also a wiki page[1] that describes how to call C++ from Haskell.</div><div><br></div><div>Best regards,</div><div>Alexey</div><div><br></div><div>[1]: <a href="https://wiki.haskell.org/CPlusPlusFromHaskell" target="_blank">https://wiki.haskell.org/CPlusPlusFromHaskell</a></div></div><div><div><div class="gmail_extra"><br><div class="gmail_quote">2015-01-26 23:27 GMT+02:00 Arjun Comar <span dir="ltr"><<a href="mailto:nrujac@gmail.com" target="_blank">nrujac@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Richard,<br>If you create a straight C header that's able to call into the C++ library then what you're trying to do will work. This is the standard (though frustrating) approach to using C++ libraries within Haskell. I can send you an example cabal file for building the Haskell code against the C headers and archives if you'd like. There isn't much more to it than what you're already doing though. The key is that the headers have to be pure C and you have to force cabal to build the C code with g++ (since the implementing .c files will necessarily make C++ calls).<br><br>Thanks,<br>Arjun<br></div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 26, 2015 at 11:29 AM, Donn Cave <span dir="ltr"><<a href="mailto:donn@avvanta.com" target="_blank">donn@avvanta.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Quoth Richard Lewis <<a href="mailto:richard.lewis@gold.ac.uk" target="_blank">richard.lewis@gold.ac.uk</a>>,<br>
...<br>
<span>> There's a specific question here which goes something like: how do I<br>
> get hsc2hs to compile code that uses STL headers?<br>
<br>
</span>For me, that would be the first thing to fix.  Use #ifdef __cplusplus,<br>
or just reorganize, so the hsc2hs-generated files are just C.<br>
You wrote your wrapper library for the C++ part, and if the callers<br>
need to be C++ it kind of defeats the purpose.<br>
<br>
>From there, I would have guessed "extra-libraries: stdc++" would do it,<br>
(assuming you have also arranged to get your wrapper library in there),<br>
but I'm no cabal expert.  I use C++ more or less like you're doing, so<br>
there's hope, but I haven't gotten around to the cabal part of the<br>
exercise.<br>
<span><font color="#888888"><br>
        Donn<br>
</font></span><div><div>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</div></div></blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br></div>
</div></div></blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">Ian Ross   Tel: +43(0)6804451378   <a href="mailto:ian@skybluetrades.net" target="_blank">ian@skybluetrades.net</a>   <a href="http://www.skybluetrades.net" target="_blank">www.skybluetrades.net</a></div>
</div>