Thanks,<br><br>I found on one site how to compile after creating the stub files with GHC:<br><br>First step:<br><b>ghc -c -ffi haskell_file.hs</b><br>Second step - here it is important to know and write where are the ghc libraries:<br>
<b>gcc -I /usr/local/lib/ghc-5.04.3/include -c C_file.c </b><br>After that it is important to link my creted C_file with the stub file and compile it:<br><b>ghc -no-hs-main -o C_file C_file.o haskell_file.o haskell_file_stub.o</b><br>
<br>The final result is C_file execution file...just enter C_file and the program is running correctly.<br><br>This information: how to compile and to link C with Haskell and to call a Haskell funtion from C was quite difficult.<br>
But here is my result of googling throw the internet and to find something usefull.<br><br>Next challange: link C++ with C and creating a usefull documentation and put it online!<br><br>Ciao,<br>Miguel Lordelo.<br><br><br>
<br><br><div class="gmail_quote">On Fri, Apr 18, 2008 at 3:33 PM, Alfonso Acosta <<a href="mailto:alfonso.acosta@gmail.com">alfonso.acosta@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Although you could use gcc to link the code I wouldn't recommend it<br>
(mainly for the problems you are currently having)<br>
<br>
SImply call GHC to compile both the C and Haskell code. It will take<br>
care of finding the headers and supplying the necessary linker<br>
arguments.<br>
<br>
ghc -ffi -c foo.hs myfoo_c.c<br>
<br>
BTW, you don't need to compile viaC<br>
<br>
2008/4/17 Miguel Lordelo <<a href="mailto:miguellordelo@gmail.com">miguellordelo@gmail.com</a>>:<br>
<div><div></div><div class="Wj3C7c">> 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 -> IO Int<br>
><br>
> foo :: Int -> IO Int<br>
> foo n = return (length (f n))<br>
><br>
> f :: Int -> [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<br>
> foo function (implemented in Haskell).<br>
> (you can see this example on<br>
> <a href="http://www.haskell.org/ghc/docs/latest/html/users_guide/ffi-ghc.html" target="_blank">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<br>
> 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<br>
> it was the only idea that occurs at the moment)<br>
> <a href="http://www.koders.com/c/fidD0593B84C41CA71319BB079EFD0A2C80211C9337.aspx" target="_blank">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'<br>
> myfoo_c.c:(.text+0x31): undefined reference to `foo'<br>
> myfoo_c.c:(.text+0x50): undefined reference to `hs_exit'<br>
> collect2: ld returned 1 exit status<br>
><br>
> These functions are necessary to setup GHC runtime (see:<br>
> <a href="http://www.haskell.org/ghc/docs/latest/html/users_guide/ffi-ghc.html" target="_blank">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>
><br>
><br>
> On Wed, Apr 16, 2008 at 9:16 PM, Isaac Dupree <<a href="mailto:isaacdupree@charter.net">isaacdupree@charter.net</a>><br>
> wrote:<br>
><br>
> > perhaps<br>
> ><br>
> > haskell:<br>
> > foreign export "foo_func" foo :: Int -> IO Int<br>
> > -- I forget the rest of the syntax here<br>
> ><br>
> > C++:<br>
> ><br>
> > extern "C" {<br>
> > int foo_func(int i);<br>
> > }<br>
> ><br>
> > int some_cplusplus_function() {<br>
> > int bat = 3;<br>
> > int blah = foo_func(bat);<br>
> > return blah;<br>
> > }<br>
> ><br>
> ><br>
> > Is that all you need to do?<br>
> ><br>
> ><br>
> > Miguel Lordelo wrote:<br>
> ><br>
> > ><br>
> > ><br>
> > ><br>
> > > Hi all,<br>
> > ><br>
> > > Well...somehow I'm a beginner in Haskell. But actually my interest in<br>
> > > Haskell will increase if it is possible to call a haskell function in<br>
> C++.<br>
> > > Something like GreenCard ( <a href="http://www.haskell.org/greencard/" target="_blank">http://www.haskell.org/greencard/</a> )<br>
> simplifying<br>
> > > the task of interfacing Haskell programs to external libraries<br>
> (usually).<br>
> > > But is there also a task to interface a foreign language with Haskell,<br>
> 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<br>
> testing<br>
> > > with Frotran and MatLab and I want to show them the power of haskell and<br>
> the<br>
> > > software which we are using is implemented in C++ (there is the reason<br>
> to<br>
> > > make Haskel -> C++).<br>
> > ><br>
> > > I read somewhere that the only way for C++ calling a haskell function is<br>
> to<br>
> > > create a binding between Haskell and C and from C to C++, but a easy<br>
> "Hello<br>
> > > World" example was not there.<br>
> > > Unfortunatelly I couldn't found anything usefull, like an complete<br>
> 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>
> > > ------------------------------------------------------------------------<br>
> > ><br>
> > ><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>
> ><br>
> ><br>
><br>
><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>
><br>
</div></div></blockquote></div><br>