Re[2]: [Haskell-cafe] C++ interface with Haskell

2008-04-18 Thread Bulat Ziganshin
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 [EMAIL PROTECTED] 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 [EMAIL PROTECTED]:
  
 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 [EMAIL PROTECTED]
  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 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 

Re[2]: [Haskell-cafe] C++ interface with Haskell

2008-04-18 Thread Bulat Ziganshin
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 [EMAIL PROTECTED]:
 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 [EMAIL PROTECTED]
 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 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


 

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe


-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: Re[2]: [Haskell-cafe] C++ interface with Haskell

2008-04-18 Thread Evan Laforge
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 :)
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: Re[2]: [Haskell-cafe] C++ interface with Haskell

2008-04-18 Thread Don Stewart
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
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe