I'm building an application in Qt
(C++
) which uses library written in Haskell as a backend. How can I export interface of Haskell
library to C++
?
When functions in haskell library are using simple types like int
or float
it's not a problem but what when they use structures as an arguments? What is the most scalable solution in this case?
You'll need to write haskell code to marshal those datastructures into ones you can use in Haskell. The c2hs tool is very helpful in this regard: http://hackage.haskell.org/package/c2hs
While the tool appears to be designed for calling C from Haskell, it is also very useful for the opposite in one sense -- it allows you to marshall complex C types into Haskell types. Since the functions you expose will need to take C types (typically pointers to complex structures), then you can A) construct appropriate C structures, and B) use the features c2hs provides in order to then marshall those structures into Haskell in your exposed functions, so that you can work with them.
Also note that with c2hs style generated tools, you don't need to marshall everything. You can just generate accessors to peek into the parts of C data structures that you actually care about.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With