Well Isaac...I became now a little bit smarter then yesterday!!!<br><br>I show you the example that I found and on which I´m working with.<br><br>File: foo.hs<br>module Foo where<br><br>foreign export ccall foo :: Int -&gt; IO Int<br>
<br>foo :: Int -&gt; IO Int<br>foo n = return (length (f n))<br><br>f :: Int -&gt; [Int]<br>f 0 = []<br>f n = n:(f (n-1))<br><br>To get the C wrapper you insert the following command:<br>ghc -ffi -fvia-C -C foo.hs<br><br>
After execution you will have these following additional files:<br><br>foo.hc<br>foo.hi<br>foo_stub.c<br>foo_stub.h<br>foo_stub.o<br><br>What I did next was to create a file named: myfoo_c.c, where I will call the foo function (implemented in Haskell).<br>
(you can see this example on <a href="http://www.haskell.org/ghc/docs/latest/html/users_guide/ffi-ghc.html">http://www.haskell.org/ghc/docs/latest/html/users_guide/ffi-ghc.html</a> )<br>But the problem is to compile with gcc (must I put any flag or whatever set something)<br>
<br>The gcc output is:<br>myfoo_c.c:2:19: error: HsFFI.h: No such file or directory<br><br>I downloaded this header file from: (I know that is not the correct way, but it was the only idea that occurs at the moment)<br><a href="http://www.koders.com/c/fidD0593B84C41CA71319BB079EFD0A2C80211C9337.aspx">http://www.koders.com/c/fidD0593B84C41CA71319BB079EFD0A2C80211C9337.aspx</a><br>
<br>I compiled again and the following return error appears:<br>myfoo_c.c:(.text+0x1c): undefined reference to `hs_init&#39;<br>myfoo_c.c:(.text+0x31): undefined reference to `foo&#39;<br>myfoo_c.c:(.text+0x50): undefined reference to `hs_exit&#39;<br>
collect2: ld returned 1 exit status<br><br>These functions are necessary to setup GHC runtime (see: <a href="http://www.haskell.org/ghc/docs/latest/html/users_guide/ffi-ghc.html">http://www.haskell.org/ghc/docs/latest/html/users_guide/ffi-ghc.html</a> )<br>
<br>What I want to know is how to compile myfoo_c.c?! Is it with GCC or GHC?!<br><br>Chears,<br>Miguel Lordelo.<br><br><br><div class="gmail_quote">On Wed, Apr 16, 2008 at 9:16 PM, Isaac Dupree &lt;<a href="mailto:isaacdupree@charter.net">isaacdupree@charter.net</a>&gt; wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">perhaps<br>
<br>
haskell:<br>
foreign export &quot;foo_func&quot; foo :: Int -&gt; IO Int<br>
-- I forget the rest of the syntax here<br>
<br>
C++:<br>
<br>
extern &quot;C&quot; {<br>
int foo_func(int i);<br>
}<br>
<br>
int some_cplusplus_function() {<br>
 &nbsp;int bat = 3;<br>
 &nbsp;int blah = foo_func(bat);<br>
 &nbsp;return blah;<br>
}<br>
<br>
<br>
Is that all you need to do?<br>
<br>
<br>
Miguel Lordelo wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div><div></div><div class="Wj3C7c">
Hi all,<br>
<br>
Well...somehow I&#39;m a beginner in Haskell. But actually my interest in<br>
Haskell will increase if it is possible to call a haskell function in C++.<br>
Something like GreenCard ( <a href="http://www.haskell.org/greencard/" target="_blank">http://www.haskell.org/greencard/</a> ) simplifying<br>
the task of interfacing Haskell programs to external libraries (usually).<br>
But is there also a task to interface a foreign language with Haskell, but<br>
calling Haskell functions. Or c2hs which is an interface generator that<br>
simplifies the development of Haskell bindings to C libraries.<br>
<br>
I want to know this, because in my company some guys are doing some testing<br>
with Frotran and MatLab and I want to show them the power of haskell and the<br>
software which we are using is implemented in C++ (there is the reason to<br>
make Haskel -&gt; C++).<br>
<br>
I read somewhere that the only way for C++ calling a haskell function is to<br>
create a binding between Haskell and C and from C to C++, but a easy &quot;Hello<br>
World&quot; example was not there.<br>
Unfortunatelly I couldn&#39;t found anything usefull, like an complete example,<br>
or how to compile the code from haskell to C to C++.<br>
<br>
Can sombody help me, please :P<br>
<br>
Chears,<br>
Miguel Lordelo.<br>
<br>
<br>
<br></div></div>
------------------------------------------------------------------------<div class="Ih2E3d"><br>
<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>
</div></blockquote>
<br>
</blockquote></div><br>