
Hi all, Well...somehow I'm a beginner in Haskell. But actually my interest in Haskell will increase if it is possible to call a haskell function in C++. Something like GreenCard ( http://www.haskell.org/greencard/ ) simplifying the task of interfacing Haskell programs to external libraries (usually). But is there also a task to interface a foreign language with Haskell, but calling Haskell functions. Or c2hs which is an interface generator that simplifies the development of Haskell bindings to C libraries. I want to know this, because in my company some guys are doing some testing with Frotran and MatLab and I want to show them the power of haskell and the software which we are using is implemented in C++ (there is the reason to make Haskel -> C++). I read somewhere that the only way for C++ calling a haskell function is to create a binding between Haskell and C and from C to C++, but a easy "Hello World" example was not there. Unfortunatelly I couldn't found anything usefull, like an complete example, or how to compile the code from haskell to C to C++. Can sombody help me, please :P Chears, Miguel Lordelo.

write the C wrapper that calls haskell, then link that to your C++ objects
I think what you're really asking is how to call C from C++
-Dan
2008/4/16 Miguel Lordelo
Hi all,
Well...somehow I'm a beginner in Haskell. But actually my interest in Haskell will increase if it is possible to call a haskell function in C++. Something like GreenCard ( http://www.haskell.org/greencard/ ) simplifying the task of interfacing Haskell programs to external libraries (usually). But is there also a task to interface a foreign language with Haskell, but calling Haskell functions. Or c2hs which is an interface generator that simplifies the development of Haskell bindings to C libraries.
I want to know this, because in my company some guys are doing some testing with Frotran and MatLab and I want to show them the power of haskell and the software which we are using is implemented in C++ (there is the reason to make Haskel -> C++).
I read somewhere that the only way for C++ calling a haskell function is to create a binding between Haskell and C and from C to C++, but a easy "Hello World" example was not there. Unfortunatelly I couldn't found anything usefull, like an complete example, or how to compile the code from haskell to C to C++.
Can sombody help me, please :P
Chears, Miguel Lordelo. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

perhaps haskell: foreign export "foo_func" foo :: Int -> IO Int -- I forget the rest of the syntax here C++: extern "C" { int foo_func(int i); } int some_cplusplus_function() { int bat = 3; int blah = foo_func(bat); return blah; } Is that all you need to do? Miguel Lordelo wrote:
Hi all,
Well...somehow I'm a beginner in Haskell. But actually my interest in Haskell will increase if it is possible to call a haskell function in C++. Something like GreenCard ( http://www.haskell.org/greencard/ ) simplifying the task of interfacing Haskell programs to external libraries (usually). But is there also a task to interface a foreign language with Haskell, but calling Haskell functions. Or c2hs which is an interface generator that simplifies the development of Haskell bindings to C libraries.
I want to know this, because in my company some guys are doing some testing with Frotran and MatLab and I want to show them the power of haskell and the software which we are using is implemented in C++ (there is the reason to make Haskel -> C++).
I read somewhere that the only way for C++ calling a haskell function is to create a binding between Haskell and C and from C to C++, but a easy "Hello World" example was not there. Unfortunatelly I couldn't found anything usefull, like an complete example, or how to compile the code from haskell to C to C++.
Can sombody help me, please :P
Chears, Miguel Lordelo.
------------------------------------------------------------------------
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

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

Although you could use gcc to link the code I wouldn't recommend it
(mainly for the problems you are currently having)
SImply call GHC to compile both the C and Haskell code. It will take
care of finding the headers and supplying the necessary linker
arguments.
ghc -ffi -c foo.hs myfoo_c.c
BTW, you don't need to compile viaC
2008/4/17 Miguel Lordelo
Well Isaac...I became now a little bit smarter then yesterday!!!
I show you the example that I found and on which I´m working with.
File: foo.hs module Foo where
foreign export ccall foo :: Int -> IO Int
foo :: Int -> IO Int foo n = return (length (f n))
f :: Int -> [Int] f 0 = [] f n = n:(f (n-1))
To get the C wrapper you insert the following command: ghc -ffi -fvia-C -C foo.hs
After execution you will have these following additional files:
foo.hc foo.hi foo_stub.c foo_stub.h foo_stub.o
What I did next was to create a file named: myfoo_c.c, where I will call the foo function (implemented in Haskell). (you can see this example on http://www.haskell.org/ghc/docs/latest/html/users_guide/ffi-ghc.html ) But the problem is to compile with gcc (must I put any flag or whatever set something)
The gcc output is: myfoo_c.c:2:19: error: HsFFI.h: No such file or directory
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) http://www.koders.com/c/fidD0593B84C41CA71319BB079EFD0A2C80211C9337.aspx
I compiled again and the following return error appears: myfoo_c.c:(.text+0x1c): undefined reference to `hs_init' myfoo_c.c:(.text+0x31): undefined reference to `foo' myfoo_c.c:(.text+0x50): undefined reference to `hs_exit' collect2: ld returned 1 exit status
These functions are necessary to setup GHC runtime (see: http://www.haskell.org/ghc/docs/latest/html/users_guide/ffi-ghc.html )
What I want to know is how to compile myfoo_c.c?! Is it with GCC or GHC?!
Chears, Miguel Lordelo.
On Wed, Apr 16, 2008 at 9:16 PM, Isaac Dupree
wrote: perhaps
haskell: foreign export "foo_func" foo :: Int -> IO Int -- I forget the rest of the syntax here
C++:
extern "C" { int foo_func(int i); }
int some_cplusplus_function() { int bat = 3; int blah = foo_func(bat); return blah; }
Is that all you need to do?
Miguel Lordelo wrote:
Hi all,
Well...somehow I'm a beginner in Haskell. But actually my interest in Haskell will increase if it is possible to call a haskell function in
Something like GreenCard ( http://www.haskell.org/greencard/ ) simplifying the task of interfacing Haskell programs to external libraries (usually). But is there also a task to interface a foreign language with Haskell, but calling Haskell functions. Or c2hs which is an interface generator that simplifies the development of Haskell bindings to C libraries.
I want to know this, because in my company some guys are doing some testing with Frotran and MatLab and I want to show them the power of haskell and
C++. the
software which we are using is implemented in C++ (there is the reason to make Haskel -> C++).
I read somewhere that the only way for C++ calling a haskell function is to create a binding between Haskell and C and from C to C++, but a easy "Hello World" example was not there. Unfortunatelly I couldn't found anything usefull, like an complete example, or how to compile the code from haskell to C to C++.
Can sombody help me, please :P
Chears, Miguel Lordelo.
------------------------------------------------------------------------
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Thanks,
I found on one site how to compile after creating the stub files with GHC:
First step:
*ghc -c -ffi haskell_file.hs*
Second step - here it is important to know and write where are the ghc
libraries:
*gcc -I /usr/local/lib/ghc-5.04.3/include -c C_file.c *
After that it is important to link my creted C_file with the stub file and
compile it:
*ghc -no-hs-main -o C_file C_file.o haskell_file.o haskell_file_stub.o*
The final result is C_file execution file...just enter C_file and the
program is running correctly.
This information: how to compile and to link C with Haskell and to call a
Haskell funtion from C was quite difficult.
But here is my result of googling throw the internet and to find something
usefull.
Next challange: link C++ with C and creating a usefull documentation and put
it online!
Ciao,
Miguel Lordelo.
On Fri, Apr 18, 2008 at 3:33 PM, Alfonso Acosta
Although you could use gcc to link the code I wouldn't recommend it (mainly for the problems you are currently having)
SImply call GHC to compile both the C and Haskell code. It will take care of finding the headers and supplying the necessary linker arguments.
ghc -ffi -c foo.hs myfoo_c.c
BTW, you don't need to compile viaC
Well Isaac...I became now a little bit smarter then yesterday!!!
I show you the example that I found and on which I´m working with.
File: foo.hs module Foo where
foreign export ccall foo :: Int -> IO Int
foo :: Int -> IO Int foo n = return (length (f n))
f :: Int -> [Int] f 0 = [] f n = n:(f (n-1))
To get the C wrapper you insert the following command: ghc -ffi -fvia-C -C foo.hs
After execution you will have these following additional files:
foo.hc foo.hi foo_stub.c foo_stub.h foo_stub.o
What I did next was to create a file named: myfoo_c.c, where I will call
foo function (implemented in Haskell). (you can see this example on http://www.haskell.org/ghc/docs/latest/html/users_guide/ffi-ghc.html ) But the problem is to compile with gcc (must I put any flag or whatever set something)
The gcc output is: myfoo_c.c:2:19: error: HsFFI.h: No such file or directory
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) http://www.koders.com/c/fidD0593B84C41CA71319BB079EFD0A2C80211C9337.aspx
I compiled again and the following return error appears: myfoo_c.c:(.text+0x1c): undefined reference to `hs_init' myfoo_c.c:(.text+0x31): undefined reference to `foo' myfoo_c.c:(.text+0x50): undefined reference to `hs_exit' collect2: ld returned 1 exit status
These functions are necessary to setup GHC runtime (see: http://www.haskell.org/ghc/docs/latest/html/users_guide/ffi-ghc.html )
What I want to know is how to compile myfoo_c.c?! Is it with GCC or GHC?!
Chears, Miguel Lordelo.
On Wed, Apr 16, 2008 at 9:16 PM, Isaac Dupree
wrote: perhaps
haskell: foreign export "foo_func" foo :: Int -> IO Int -- I forget the rest of the syntax here
C++:
extern "C" { int foo_func(int i); }
int some_cplusplus_function() { int bat = 3; int blah = foo_func(bat); return blah; }
Is that all you need to do?
Miguel Lordelo wrote:
Hi all,
Well...somehow I'm a beginner in Haskell. But actually my interest
in
Haskell will increase if it is possible to call a haskell function in C++. Something like GreenCard ( http://www.haskell.org/greencard/ ) simplifying the task of interfacing Haskell programs to external libraries (usually). But is there also a task to interface a foreign language with Haskell, but calling Haskell functions. Or c2hs which is an interface generator
2008/4/17 Miguel Lordelo
: the that simplifies the development of Haskell bindings to C libraries.
I want to know this, because in my company some guys are doing some testing with Frotran and MatLab and I want to show them the power of haskell and the software which we are using is implemented in C++ (there is the reason to make Haskel -> C++).
I read somewhere that the only way for C++ calling a haskell function is to create a binding between Haskell and C and from C to C++, but a easy "Hello World" example was not there. Unfortunatelly I couldn't found anything usefull, like an complete example, or how to compile the code from haskell to C to C++.
Can sombody help me, please :P
Chears, Miguel Lordelo.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hello Miguel, Friday, April 18, 2008, 7:06:07 PM, you wrote: you may look into my freearc.org project overall, nothing complex as far as you got it :) i use ghc -c c_file.cpp ghc --make main.hs c_file.o in order to call from C++ to Haskell or vice versa you should define function in C++ as having extern "C" linkage. i recommend you to declare function in header file which is able to compile either in C++ mode (used in first step) or C mode (used in second step, when compiling main.hs): #ifdef __cplusplus extern "C" { #endif void myfunc(void); #ifdef __cplusplus } #endif then you use either "foreign import" haskell statement to use C++ func from haskell or "foreign export" for other way. i also recommend you to use main procedure written in haskell and run from this procedure your main C function - this is the simplest way to initialize Haskell runtime system. that's all
Thanks,
I found on one site how to compile after creating the stub files with GHC:
First step: ghc -c -ffi haskell_file.hs Second step - here it is important to know and write where are the ghc libraries: gcc -I /usr/local/lib/ghc-5.04.3/include -c C_file.c After that it is important to link my creted C_file with the stub file and compile it: ghc -no-hs-main -o C_file C_file.o haskell_file.o haskell_file_stub.o
The final result is C_file execution file...just enter C_file and the program is running correctly.
This information: how to compile and to link C with Haskell and to call a Haskell funtion from C was quite difficult. But here is my result of googling throw the internet and to find something usefull.
Next challange: link C++ with C and creating a usefull documentation and put it online!
Ciao, Miguel Lordelo.
On Fri, Apr 18, 2008 at 3:33 PM, Alfonso Acosta
wrote: Although you could use gcc to link the code I wouldn't recommend it (mainly for the problems you are currently having) SImply call GHC to compile both the C and Haskell code. It will take care of finding the headers and supplying the necessary linker arguments.
ghc -ffi -c foo.hs myfoo_c.c
BTW, you don't need to compile viaC
2008/4/17 Miguel Lordelo
: Well Isaac...I became now a little bit smarter then yesterday!!!
I show you the example that I found and on which I?m working with.
File: foo.hs module Foo where
foreign export ccall foo :: Int -> IO Int
foo :: Int -> IO Int foo n = return (length (f n))
f :: Int -> [Int] f 0 = [] f n = n:(f (n-1))
To get the C wrapper you insert the following command: ghc -ffi -fvia-C -C foo.hs
After execution you will have these following additional files:
foo.hc foo.hi foo_stub.c foo_stub.h foo_stub.o
What I did next was to create a file named: myfoo_c.c, where I will call the foo function (implemented in Haskell). (you can see this example on http://www.haskell.org/ghc/docs/latest/html/users_guide/ffi-ghc.html ) But the problem is to compile with gcc (must I put any flag or whatever set something)
The gcc output is: myfoo_c.c:2:19: error: HsFFI.h: No such file or directory
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) http://www.koders.com/c/fidD0593B84C41CA71319BB079EFD0A2C80211C9337.aspx
I compiled again and the following return error appears: myfoo_c.c:(.text+0x1c): undefined reference to `hs_init' myfoo_c.c:(.text+0x31): undefined reference to `foo' myfoo_c.c:(.text+0x50): undefined reference to `hs_exit' collect2: ld returned 1 exit status
These functions are necessary to setup GHC runtime (see: http://www.haskell.org/ghc/docs/latest/html/users_guide/ffi-ghc.html )
What I want to know is how to compile myfoo_c.c?! Is it with GCC or GHC?!
Chears, Miguel Lordelo.
On Wed, Apr 16, 2008 at 9:16 PM, Isaac Dupree
wrote: perhaps
haskell: foreign export "foo_func" foo :: Int -> IO Int -- I forget the rest of the syntax here
C++:
extern "C" { int foo_func(int i); }
int some_cplusplus_function() { int bat = 3; int blah = foo_func(bat); return blah; }
Is that all you need to do?
Miguel Lordelo wrote:
Hi all,
Well...somehow I'm a beginner in Haskell. But actually my interest in Haskell will increase if it is possible to call a haskell function in
Something like GreenCard ( http://www.haskell.org/greencard/ ) simplifying the task of interfacing Haskell programs to external libraries (usually). But is there also a task to interface a foreign language with Haskell, but calling Haskell functions. Or c2hs which is an interface generator that simplifies the development of Haskell bindings to C libraries.
I want to know this, because in my company some guys are doing some testing with Frotran and MatLab and I want to show them the power of haskell and
C++. the
software which we are using is implemented in C++ (there is the reason to make Haskel -> C++).
I read somewhere that the only way for C++ calling a haskell function is to create a binding between Haskell and C and from C to C++, but a easy "Hello World" example was not there. Unfortunatelly I couldn't found anything usefull, like an complete example, or how to compile the code from haskell to C to C++.
Can sombody help me, please :P
Chears, Miguel Lordelo.
------------------------------------------------------------------------
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

if you'd normally be linking using g++, you'll need (IIRC) -lstdc++ added to linking-ghc's command line Alfonso Acosta wrote:
Although you could use gcc to link the code I wouldn't recommend it (mainly for the problems you are currently having)
SImply call GHC to compile both the C and Haskell code. It will take care of finding the headers and supplying the necessary linker arguments.
ghc -ffi -c foo.hs myfoo_c.c
BTW, you don't need to compile viaC
2008/4/17 Miguel Lordelo
: Well Isaac...I became now a little bit smarter then yesterday!!!
I show you the example that I found and on which I´m working with.
File: foo.hs module Foo where
foreign export ccall foo :: Int -> IO Int
foo :: Int -> IO Int foo n = return (length (f n))
f :: Int -> [Int] f 0 = [] f n = n:(f (n-1))
To get the C wrapper you insert the following command: ghc -ffi -fvia-C -C foo.hs
After execution you will have these following additional files:
foo.hc foo.hi foo_stub.c foo_stub.h foo_stub.o
What I did next was to create a file named: myfoo_c.c, where I will call the foo function (implemented in Haskell). (you can see this example on http://www.haskell.org/ghc/docs/latest/html/users_guide/ffi-ghc.html ) But the problem is to compile with gcc (must I put any flag or whatever set something)
The gcc output is: myfoo_c.c:2:19: error: HsFFI.h: No such file or directory
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) http://www.koders.com/c/fidD0593B84C41CA71319BB079EFD0A2C80211C9337.aspx
I compiled again and the following return error appears: myfoo_c.c:(.text+0x1c): undefined reference to `hs_init' myfoo_c.c:(.text+0x31): undefined reference to `foo' myfoo_c.c:(.text+0x50): undefined reference to `hs_exit' collect2: ld returned 1 exit status
These functions are necessary to setup GHC runtime (see: http://www.haskell.org/ghc/docs/latest/html/users_guide/ffi-ghc.html )
What I want to know is how to compile myfoo_c.c?! Is it with GCC or GHC?!
Chears, Miguel Lordelo.
On Wed, Apr 16, 2008 at 9:16 PM, Isaac Dupree
wrote: perhaps
haskell: foreign export "foo_func" foo :: Int -> IO Int -- I forget the rest of the syntax here
C++:
extern "C" { int foo_func(int i); }
int some_cplusplus_function() { int bat = 3; int blah = foo_func(bat); return blah; }
Is that all you need to do?
Miguel Lordelo wrote:
Hi all,
Well...somehow I'm a beginner in Haskell. But actually my interest in Haskell will increase if it is possible to call a haskell function in
Something like GreenCard ( http://www.haskell.org/greencard/ ) simplifying the task of interfacing Haskell programs to external libraries (usually). But is there also a task to interface a foreign language with Haskell, but calling Haskell functions. Or c2hs which is an interface generator that simplifies the development of Haskell bindings to C libraries.
I want to know this, because in my company some guys are doing some testing with Frotran and MatLab and I want to show them the power of haskell and
C++. the
software which we are using is implemented in C++ (there is the reason to make Haskel -> C++).
I read somewhere that the only way for C++ calling a haskell function is to create a binding between Haskell and C and from C to C++, but a easy "Hello World" example was not there. Unfortunatelly I couldn't found anything usefull, like an complete example, or how to compile the code from haskell to C to C++.
Can sombody help me, please :P
Chears, Miguel Lordelo.
------------------------------------------------------------------------
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hello Isaac, Friday, April 18, 2008, 7:27:56 PM, you wrote: absolutely true! it's required if you use new/delete and other things supported by c++ RTS
if you'd normally be linking using g++, you'll need (IIRC) -lstdc++ added to linking-ghc's command line
Alfonso Acosta wrote:
Although you could use gcc to link the code I wouldn't recommend it (mainly for the problems you are currently having)
SImply call GHC to compile both the C and Haskell code. It will take care of finding the headers and supplying the necessary linker arguments.
ghc -ffi -c foo.hs myfoo_c.c
BTW, you don't need to compile viaC
2008/4/17 Miguel Lordelo
: Well Isaac...I became now a little bit smarter then yesterday!!!
I show you the example that I found and on which I?m working with.
File: foo.hs module Foo where
foreign export ccall foo :: Int -> IO Int
foo :: Int -> IO Int foo n = return (length (f n))
f :: Int -> [Int] f 0 = [] f n = n:(f (n-1))
To get the C wrapper you insert the following command: ghc -ffi -fvia-C -C foo.hs
After execution you will have these following additional files:
foo.hc foo.hi foo_stub.c foo_stub.h foo_stub.o
What I did next was to create a file named: myfoo_c.c, where I will call the foo function (implemented in Haskell). (you can see this example on http://www.haskell.org/ghc/docs/latest/html/users_guide/ffi-ghc.html ) But the problem is to compile with gcc (must I put any flag or whatever set something)
The gcc output is: myfoo_c.c:2:19: error: HsFFI.h: No such file or directory
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) http://www.koders.com/c/fidD0593B84C41CA71319BB079EFD0A2C80211C9337.aspx
I compiled again and the following return error appears: myfoo_c.c:(.text+0x1c): undefined reference to `hs_init' myfoo_c.c:(.text+0x31): undefined reference to `foo' myfoo_c.c:(.text+0x50): undefined reference to `hs_exit' collect2: ld returned 1 exit status
These functions are necessary to setup GHC runtime (see: http://www.haskell.org/ghc/docs/latest/html/users_guide/ffi-ghc.html )
What I want to know is how to compile myfoo_c.c?! Is it with GCC or GHC?!
Chears, Miguel Lordelo.
On Wed, Apr 16, 2008 at 9:16 PM, Isaac Dupree
wrote: perhaps
haskell: foreign export "foo_func" foo :: Int -> IO Int -- I forget the rest of the syntax here
C++:
extern "C" { int foo_func(int i); }
int some_cplusplus_function() { int bat = 3; int blah = foo_func(bat); return blah; }
Is that all you need to do?
Miguel Lordelo wrote:
Hi all,
Well...somehow I'm a beginner in Haskell. But actually my interest in Haskell will increase if it is possible to call a haskell function in
Something like GreenCard ( http://www.haskell.org/greencard/ ) simplifying the task of interfacing Haskell programs to external libraries (usually). But is there also a task to interface a foreign language with Haskell, but calling Haskell functions. Or c2hs which is an interface generator that simplifies the development of Haskell bindings to C libraries.
I want to know this, because in my company some guys are doing some testing with Frotran and MatLab and I want to show them the power of haskell and
C++. the
software which we are using is implemented in C++ (there is the reason to make Haskel -> C++).
I read somewhere that the only way for C++ calling a haskell function is to create a binding between Haskell and C and from C to C++, but a easy "Hello World" example was not there. Unfortunatelly I couldn't found anything usefull, like an complete example, or how to compile the code from haskell to C to C++.
Can sombody help me, please :P
Chears, Miguel Lordelo.
------------------------------------------------------------------------
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

To threadjack a little bit, I've been interfacing haskell with c++. It gets awkward when the c++ structures use STL types like string and vector. Of course those are too complex for haskell to marshal to. What I've been doing is defining an XMarshal variant of the X c++ class, that uses plain c arrays. Then I marshal to that, and construct the c++ object properly from XMarshal in the c->c++ wrapper layer. On a few occasions, when the c++ class is really big and only has one STL member, I make a partially constructed c++ object, pass the array separately, and then construct the proper c++ class from the broken haskell generated one. Possibly dangerous as all get-out because I'm dealing with "unconstructed" c++ objects, but it seems to work. Passing back to haskell is easier since I can use "&*vec.begin()", which according to the internet should be safe because STL guarantees that vector contents are contiguous. I'm only saved by the fact that I don't have that many different kinds of classes to pass. This would be much more drudgery if I had more. Does anyone have a better solution or convention for marshalling c++ objects? I've also noticed warnings from g++ about hsc2hs's use of the OFFSETOF macro on c++ classes, but some googling of g++ mailing lists implied that it's harmless if you don't have virtual bases, and what sane person does, so I suppress it now :)

qdunkan:
To threadjack a little bit, I've been interfacing haskell with c++. It gets awkward when the c++ structures use STL types like string and vector. Of course those are too complex for haskell to marshal to.
What I've been doing is defining an XMarshal variant of the X c++ class, that uses plain c arrays. Then I marshal to that, and construct the c++ object properly from XMarshal in the c->c++ wrapper layer. On a few occasions, when the c++ class is really big and only has one STL member, I make a partially constructed c++ object, pass the array separately, and then construct the proper c++ class from the broken haskell generated one. Possibly dangerous as all get-out because I'm dealing with "unconstructed" c++ objects, but it seems to work.
Passing back to haskell is easier since I can use "&*vec.begin()", which according to the internet should be safe because STL guarantees that vector contents are contiguous.
I'm only saved by the fact that I don't have that many different kinds of classes to pass. This would be much more drudgery if I had more. Does anyone have a better solution or convention for marshalling c++ objects?
I've also noticed warnings from g++ about hsc2hs's use of the OFFSETOF macro on c++ classes, but some googling of g++ mailing lists implied that it's harmless if you don't have virtual bases, and what sane person does, so I suppress it now :)
Would someone like to summarise the current approaches to combining Haskell & C++ on the Haskell wiki, even if just in bullet points? -- Don

Hello Don, Saturday, April 19, 2008, 12:08:11 AM, you wrote:
Would someone like to summarise the current approaches to combining Haskell & C++ on the Haskell wiki, even if just in bullet points?
started at http://haskell.org/haskellwiki/IO_inside#Interfacing_with_foreign_evil -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Evan Laforge wrote:
To threadjack a little bit, I've been interfacing haskell with c++. It gets awkward when the c++ structures use STL types like string and vector. Of course those are too complex for haskell to marshal to.
What I've been doing is defining an XMarshal variant of the X c++ class, that uses plain c arrays. Then I marshal to that, and construct the c++ object properly from XMarshal in the c->c++ wrapper layer. On a few occasions, when the c++ class is really big and only has one STL member, I make a partially constructed c++ object, pass the array separately, and then construct the proper c++ class from the broken haskell generated one. Possibly dangerous as all get-out because I'm dealing with "unconstructed" c++ objects, but it seems to work.
you mean, you hack around with the internal representation of those structures? Well, if you want to avoid double-copying, C++ can't access Haskell sequences, and Haskell can't access C++ sequences, I guess I don't see an alternative.
Passing back to haskell is easier since I can use "&*vec.begin()", which according to the internet should be safe because STL guarantees that vector contents are contiguous.
safe until either: the vector's contents change, if Haskell is assuming it's immutable, or more seriously, if the vector's length is changed, the pointers are invalidated and it might crash (due to reallocating for a bigger continuous memory chunk)
I'm only saved by the fact that I don't have that many different kinds of classes to pass. This would be much more drudgery if I had more. Does anyone have a better solution or convention for marshalling c++ objects?
not "better", but, you could wrap the methods of the class and call back into C++ (through C wrappers) to do anything with the class, if it suited your purposes better and wasn't too slow
I've also noticed warnings from g++ about hsc2hs's use of the OFFSETOF macro on c++ classes, but some googling of g++ mailing lists implied that it's harmless if you don't have virtual bases, and what sane person does, so I suppress it now :)

you mean, you hack around with the internal representation of those structures? Well, if you want to avoid double-copying, C++ can't access Haskell sequences, and Haskell can't access C++ sequences, I guess I don't see an alternative.
I don't really mind double copying, but having to declare c variants (i.e. no stl) of all the c++ structures and then copy from one to the other is a little tedious. I was hoping there was some clever trick to make that easier...
Passing back to haskell is easier since I can use "&*vec.begin()", which according to the internet should be safe because STL guarantees that vector contents are contiguous.
safe until either: the vector's contents change, if Haskell is assuming it's immutable,
Well, naturally I peekArray before letting c++ have it back.
I'm only saved by the fact that I don't have that many different kinds of classes to pass. This would be much more drudgery if I had more. Does anyone have a better solution or convention for marshalling c++ objects?
not "better", but, you could wrap the methods of the class and call back into C++ (through C wrappers) to do anything with the class, if it suited your purposes better and wasn't too slow
Yeah, I was thinking of that, but it seemed like even more of a hassle. However, a more practical variant might be to write a haskell interface to marshal to vectors and strings, probably involving some void pointer sketchiness since naturally template types can't be expressed in a C function signature, and then poke those into the struct when I pass it. That would only work for vectors stored by pointer of course.

you could write a C++ function to marshal a Sequence (or any Container IIRC, maybe Forward Container) to a vector (or whatever you wanted -- there are choices), and then okay let's see if I remember C++ well enough This design has extra copying. but anyway template<typename Container> std::vector<typename Container::value_type> container_to_vector(Container const& c) { return std::vector<typename Container::value_type>(c.begin(), c.end()); } and template<typename Container> Container vector_to_sequence(std::vector<typename Container::value_type> const& c) { return Container(c.begin(), c.end()); } extern "C" { /* the temporary returned variable doesn't last long enough here */ (char*, int)/*I know C++ doesn't have this syntax of tuples*/ string_to_array(std::string const& s) { return (&*container_to_vector(s).begin()) } } In other words I suspect that it's possible with a minimum of boilerplate per type, (possibly including the use of macros), but I'm not sure exactly what you need to do, and I got tired of being a C++-fu expert a few years ago
participants (7)
-
Alfonso Acosta
-
Bulat Ziganshin
-
Dan Mead
-
Don Stewart
-
Evan Laforge
-
Isaac Dupree
-
Miguel Lordelo