caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] interfacing C and OCaml
@ 2003-06-26 14:42 Jung Woon Ho
  2003-06-26 15:14 ` Lex Stein
  2003-06-26 19:02 ` Jean-Baptiste Rouquier
  0 siblings, 2 replies; 16+ messages in thread
From: Jung Woon Ho @ 2003-06-26 14:42 UTC (permalink / raw)
  To: caml-list

Hi,

Can anybody explain me if there is anyway that I can pass in
a (int*int*int*int*int) from a C function to OCaml.

Thank you.

_________________________________________________________________
The new MSN 8: smart spam protection and 2 months FREE*  
http://join.msn.com/?page=features/junkmail

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Caml-list] interfacing C and OCaml
  2003-06-26 14:42 [Caml-list] interfacing C and OCaml Jung Woon Ho
@ 2003-06-26 15:14 ` Lex Stein
  2003-06-26 19:02 ` Jean-Baptiste Rouquier
  1 sibling, 0 replies; 16+ messages in thread
From: Lex Stein @ 2003-06-26 15:14 UTC (permalink / raw)
  To: Jung Woon Ho; +Cc: caml-list


Hi, In the C code allocate a memory block value of size 5 with tag 0.

CAMLparam0 ();
CAMLlocal2 (v, v1);
static value * clos;

v = alloc_tuple (5);
/* assign to the fields using the Store_Field macro */

clos = caml_named_value ("c_function_name")

/* where c_function_name is registered from within the Caml code
   via (Callback.register "c_function_name" function_name)
Then just call the Caml routine via callback like so: */

v1 = callback (*clos, v);

Remember to return from the C function with one of the CAMLreturn
macros to be friendly with the garbage collector.

Let me know if you have any further questions/problems with Caml/C
interaction. I'm happy to help.

Lex

--
Lex Stein                     http://www.eecs.harvard.edu/~stein/
stein@eecs.harvard.edu        TEL: 617-233-0246


On Thu, 26 Jun 2003, Jung Woon Ho wrote:

> Hi,
>
> Can anybody explain me if there is anyway that I can pass in
> a (int*int*int*int*int) from a C function to OCaml.
>
> Thank you.
>
> _________________________________________________________________
> The new MSN 8: smart spam protection and 2 months FREE*
> http://join.msn.com/?page=features/junkmail
>
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Caml-list] interfacing C and OCaml
  2003-06-26 14:42 [Caml-list] interfacing C and OCaml Jung Woon Ho
  2003-06-26 15:14 ` Lex Stein
@ 2003-06-26 19:02 ` Jean-Baptiste Rouquier
  2003-06-27  5:24   ` Re[2]: " Mikhail Fedotov
  1 sibling, 1 reply; 16+ messages in thread
From: Jean-Baptiste Rouquier @ 2003-06-26 19:02 UTC (permalink / raw)
  To: caml-list

> Can anybody explain me if there is anyway that I can pass in
> a (int*int*int*int*int) from a C function to OCaml.

Do you want to call C auxiliary function from Caml, or are you writing your
main programm in C and using some Caml functions ?
Both are very well explained in the manual, though it may take a while to
read it.
Don't hesitate to ask me for a complete example if your main programm is in
Caml (I haven't learn yet how to write the main programm in C).



------------------------------------------------
À propos, I have some questions about the manual :
1. Should I use
CAMLprim value input(value channel, value buffer, value offset, value
length)
{
  return ...
}

as in 18.1.2 in the manual, or

void foo (value v1, value v2, value v3)
{
CAMLparam3 (v1, v2, v3);
...
CAMLreturn0;
}

as in 18.5.1, or both ? I think at least the second.


------------------------------------------------
2.
I use
  alloc(2*n, Double_array_tag.)
to create a float array of size n (then I initialize it), is it correct ?


------------------------------------------------
3. Is it correct to store p in the custom block v by
CAMLlocal(v);
v = alloc_custom(ops, 4, 0, 1);
(my_type *) Data_custom_val(v) = p;
?
When does the parameter "size" (here "4") vary ?

Thanks,
Jean-Baptiste.








-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re[2]: [Caml-list] interfacing C and OCaml
  2003-06-26 19:02 ` Jean-Baptiste Rouquier
@ 2003-06-27  5:24   ` Mikhail Fedotov
  2003-06-27  8:31     ` Jean-Baptiste Rouquier
  0 siblings, 1 reply; 16+ messages in thread
From: Mikhail Fedotov @ 2003-06-27  5:24 UTC (permalink / raw)
  To: Jean-Baptiste Rouquier; +Cc: caml-list

Hi!

JBR> 1. Should I use
JBR> CAMLprim value input(value channel, value buffer, value offset, value
JBR> length)
JBR> {
JBR>   return ...
JBR> }

Yes. This function can be called from other C function or from OCaml.

JBR> as in 18.1.2 in the manual, or

JBR> void foo (value v1, value v2, value v3)
JBR> {
JBR> CAMLparam3 (v1, v2, v3);
JBR> ...
JBR> CAMLreturn0;
JBR> }

This if for functions called from C only. Not suitable for use from
OCaml directly.

Should be specified in the manual, I believe, but I've got no response
in the list to my comment on this. I hope it will be in the manual for
the OCaml 3.07.

JBR> ------------------------------------------------
JBR> 3. Is it correct to store p in the custom block v by
JBR> CAMLlocal(v);
JBR> v = alloc_custom(ops, 4, 0, 1);
JBR> (my_type *) Data_custom_val(v) = p;
JBR> ?
JBR> When does the parameter "size" (here "4") vary ?

CAMLlocal(v);
my_type *t;
my_type r;

v = alloc_custom(ops, 1 + sizeof(my_type), 1, 10);
t = (my_type *) Data_custom_val(v);

t->some_int_member = 7;

Mikhail

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Caml-list] interfacing C and OCaml
  2003-06-27  5:24   ` Re[2]: " Mikhail Fedotov
@ 2003-06-27  8:31     ` Jean-Baptiste Rouquier
  2003-06-27  8:59       ` Re[2]: " Mikhail Fedotov
  0 siblings, 1 reply; 16+ messages in thread
From: Jean-Baptiste Rouquier @ 2003-06-27  8:31 UTC (permalink / raw)
  To: caml-list

> JBR> 1. Should I use
> JBR> CAMLprim value input(value channel, value buffer, value offset, value
> JBR> length)
> JBR> {
> JBR>   return Val_long (...);
> JBR> }
>
> Yes. This function can be called from other C function or from OCaml.
>
> JBR> as in 18.1.2 in the manual, or
>
> JBR> void foo (value v1, value v2, value v3)
> JBR> {
> JBR> CAMLparam3 (v1, v2, v3);
> JBR> ...
> JBR> CAMLreturn0;
> JBR> }
>
> This if for functions called from C only. Not suitable for use from
> OCaml directly.
>
> Should be specified in the manual, I believe, but I've got no response
> in the list to my comment on this. I hope it will be in the manual for
> the OCaml 3.07.
>


Strange !
The complete example given in 18.6 contains :

curses.mli :
external initscr: unit -> window = "curses_initscr"

curses.c ("curses.o" in the manual) :
value curses_initscr(value unit)
{
CAMLparam1 (unit);
CAMLreturn ((value) initscr());
}

curses.ml :
open Curses
let main_window = initscr () in


So functions using CAMLparam and CAMLreturn are called from OCaml directly.
Or am I completely wrong ?
I'm worried about the GC (for now, my code seems to run correctly, but use
perhaps to much memory).


Thanks,
JBR

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re[2]: [Caml-list] interfacing C and OCaml
  2003-06-27  8:31     ` Jean-Baptiste Rouquier
@ 2003-06-27  8:59       ` Mikhail Fedotov
  2003-06-27 12:57         ` art yerkes
  0 siblings, 1 reply; 16+ messages in thread
From: Mikhail Fedotov @ 2003-06-27  8:59 UTC (permalink / raw)
  To: Jean-Baptiste Rouquier; +Cc: caml-list

Hello Jean-Baptiste,

Friday, June 27, 2003, 12:31:44 PM, you wrote:

>> JBR> 1. Should I use
>> JBR> CAMLprim value input(value channel, value buffer, value offset, value
>> JBR> length)
>> JBR> {
>> JBR>   return Val_long (...);
>> JBR> }
>>
>> Yes. This function can be called from other C function or from OCaml.
>>
>> JBR> as in 18.1.2 in the manual, or
>>
>> JBR> void foo (value v1, value v2, value v3)
>> JBR> {
>> JBR> CAMLparam3 (v1, v2, v3);
>> JBR> ...
>> JBR> CAMLreturn0;
>> JBR> }
>>
>> This if for functions called from C only. Not suitable for use from
>> OCaml directly.
>>
>> Should be specified in the manual, I believe, but I've got no response
>> in the list to my comment on this. I hope it will be in the manual for
>> the OCaml 3.07.
>>


JBR> Strange !
JBR> The complete example given in 18.6 contains :

JBR> curses.mli :
JBR> external initscr: unit -> window = "curses_initscr"

JBR> curses.c ("curses.o" in the manual) :
JBR> value curses_initscr(value unit)
JBR> {
JBR> CAMLparam1 (unit);
JBR> CAMLreturn ((value) initscr());
JBR> }

JBR> curses.ml :
JBR> open Curses
JBR> let main_window = initscr () in


JBR> So functions using CAMLparam and CAMLreturn are called from OCaml directly.
JBR> Or am I completely wrong ?
JBR> I'm worried about the GC (for now, my code seems to run correctly, but use
JBR> perhaps to much memory).

Sorry, I've made a mistake myself. I've been talking mostly about "void" vs "value"
issue in your examples, but this is already addressed in the manual by stating that
the primitive function must return value. The documentation lacks introduction into
the macroses "CAMLprim", "CAMLextern" etc, but they are not strictly required now
if you are not dealing with Windows DLLs.

I'd say that it is a bad style to write code without those macroses,
at least.

-- 
Best regards,
 Mikhail                            mailto:mikhail@kittown.com

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Re[2]: [Caml-list] interfacing C and OCaml
  2003-06-27  8:59       ` Re[2]: " Mikhail Fedotov
@ 2003-06-27 12:57         ` art yerkes
  2003-06-27 13:14           ` Re[4]: " Mikhail Fedotov
  0 siblings, 1 reply; 16+ messages in thread
From: art yerkes @ 2003-06-27 12:57 UTC (permalink / raw)
  To: Mikhail Fedotov; +Cc: Jean-Baptiste.Rouquier, caml-list

> Sorry, I've made a mistake myself. I've been talking mostly about "void" vs "value"
> issue in your examples, but this is already addressed in the manual by stating that
> the primitive function must return value. The documentation lacks introduction into
> the macroses "CAMLprim", "CAMLextern" etc, but they are not strictly required now
> if you are not dealing with Windows DLLs.

In my experience, it's completely safe to use the macros no matter where the
code is called from.  You can omit them only when that would be safe, (i.e.
you don't allocate anything into the caml heap).  You can get away with not
being nice to the GC only when you don't do anything that can trigger the
collector.  This means that if you do any alloc, alloc_tuple, copy_string, 
etc., then you must use them.  It's not only for windows DLLs.
-- 
`No, you don't understand,' the Knight said, looking a little vexed. 
`That's what the name is called. The name really is "The Aged Aged 
Man."'
-- Lewis Carroll

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re[4]: [Caml-list] interfacing C and OCaml
  2003-06-27 12:57         ` art yerkes
@ 2003-06-27 13:14           ` Mikhail Fedotov
  2003-06-27 17:45             ` art yerkes
  0 siblings, 1 reply; 16+ messages in thread
From: Mikhail Fedotov @ 2003-06-27 13:14 UTC (permalink / raw)
  To: art yerkes; +Cc: Jean-Baptiste.Rouquier, caml-list

Hello art,

Friday, June 27, 2003, 4:57:13 PM, you wrote:

>> Sorry, I've made a mistake myself. I've been talking mostly about "void" vs "value"
>> issue in your examples, but this is already addressed in the manual by stating that
>> the primitive function must return value. The documentation lacks introduction into
>> the macroses "CAMLprim", "CAMLextern" etc, but they are not strictly required now
>> if you are not dealing with Windows DLLs.

ay> In my experience, it's completely safe to use the macros no matter where the
ay> code is called from.  You can omit them only when that would be safe, (i.e.
ay> you don't allocate anything into the caml heap).  You can get away with not
ay> being nice to the GC only when you don't do anything that can trigger the
ay> collector.  This means that if you do any alloc, alloc_tuple, copy_string, 
ay> etc., then you must use them.  It's not only for windows DLLs.

Hmpf. I've made one more mistake by not being specific enough. You are
talking about the CAMLparam/CAMLlocal macroses, and I'm talking about
CAMLprim/CAMLextern macroses; those that I'm talking about are used
as a prefix to a function name/type, not inside the body.

If you have a "unit"-type function, you still need it to have a return
value of type "value" with the value "Val_unit". This was en error in
the first example if you would want to export that function to OCaml.

If you don't use CAMLprim macroses you are introducing the risk that
you code will need to be changed for someone to use.

BTW, if you are calling one C function from another one with passing
or returning values, you can escape from the need to use
CAMLparam/CAMLlocal by passing a pointer to the values that are
already CAMLparam-ed in the calling function, i.e. use "value*" type
for function parameters for the functions that you don't need to
export.

-- 
Best regards,
 Mikhail                            mailto:mikhail@kittown.com

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Re[4]: [Caml-list] interfacing C and OCaml
  2003-06-27 13:14           ` Re[4]: " Mikhail Fedotov
@ 2003-06-27 17:45             ` art yerkes
  0 siblings, 0 replies; 16+ messages in thread
From: art yerkes @ 2003-06-27 17:45 UTC (permalink / raw)
  To: Mikhail Fedotov; +Cc: Jean-Baptiste.Rouquier, caml-list

On Fri, 27 Jun 2003 17:14:14 +0400
Mikhail Fedotov <mikhail@kittown.com> wrote:

> Hello art,
> 
> Friday, June 27, 2003, 4:57:13 PM, you wrote:
> 
> >> Sorry, I've made a mistake myself. I've been talking mostly about "void" vs "value"
> >> issue in your examples, but this is already addressed in the manual by stating that
> >> the primitive function must return value. The documentation lacks introduction into
> >> the macroses "CAMLprim", "CAMLextern" etc, but they are not strictly required now
> >> if you are not dealing with Windows DLLs.
> 

Sorry, I did't pay attention very well.  I meant CAMLparam, CAMLreturn, etc.

-- 
`No, you don't understand,' the Knight said, looking a little vexed. 
`That's what the name is called. The name really is "The Aged Aged 
Man."'
-- Lewis Carroll

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Caml-list] Interfacing C and Ocaml
  2003-06-19 19:58 [Caml-list] Interfacing C and Ocaml Jung Woon Ho
  2003-06-19 20:06 ` Kip Macy
@ 2003-06-19 20:28 ` art yerkes
  1 sibling, 0 replies; 16+ messages in thread
From: art yerkes @ 2003-06-19 20:28 UTC (permalink / raw)
  To: Jung Woon Ho; +Cc: caml-list, unosoft

On Thu, 19 Jun 2003 19:58:16 +0000
"Jung Woon Ho" <unosoft@hotmail.com> wrote:

See: http://lists.suse.com/archive/suse-linux-e/2001-Aug/0476.html

It appears that the name of the curses library on Suse is libncurses.

> Hi, Im trying to implement a simple program that interfaces C and Ocaml.
> Here is what I do:
> 
>         ocamlc -custom -output-obj -o modcaml.o mod.ml
>         ocamlc -c modwrap.c
>         cp /usr/local/lib/ocaml/libcamlrun.a mod.a
>         ar r mod.a modcaml.o modwrap.o
> 	cc -o prog main.c mod.a -lcurses
> 
> and I get the error:
> /usr/lib/gcc-lib/i486-suse-linux/3.2/../../../../i486-suse-linux/bin/ld: 
> cannot find -lcurses
> collect2: ld returned 1 exit status
> 
> I tried cc -o prog main.c mod.a -lcurses -ltermcap and cc -o prog main.c 
> mod.a -ltermcap but i cant get the program to link.
> 
> Does anybody know how to solve this problem?
> 
> Thank you very much.
> 
> 
> 
> 
> ---------------------mod.ml-------------------------------
> (* File mod.ml -- some ``useful'' Caml functions *)
> 
> let rec fib n = if n < 2 then 1 else fib(n-1) + fib(n-2)
> 
> let format_result n = Printf.sprintf "Result is: %d\n" n
> 
> (* Export those two functions to C *)
> 
> let _ = Callback.register "fib" fib
> let _ = Callback.register "format_result" format_result
> ----------------------------------------------------------
> 
> --------------------modwrap.c-----------------------------
> /* File modwrap.c -- wrappers around the Caml functions */
> 
> #include <stdio.h>
> #include <string.h>
> #include <caml/mlvalues.h>
> #include <caml/callback.h>
> 
> int fib(int n)
> {
>   static value * fib_closure = NULL;
>   if (fib_closure == NULL) fib_closure = caml_named_value("fib");
>   return Int_val(callback(*fib_closure, Val_int(n)));
> }
> 
> char * format_result(int n)
> {
>   static value * format_result_closure = NULL;
>   if (format_result_closure == NULL)
>     format_result_closure = caml_named_value("format_result");
>   return strdup(String_val(callback(*format_result_closure, Val_int(n))));
>   /* We copy the C string returned by String_val to the C heap
>      so that it remains valid after garbage collection. */
> }
> ----------------------------------------------------------
> 
> -------------------main.c---------------------------------
> /* File main.c -- a sample client for the Caml functions */
> 
> #include <stdio.h>
> 
> int main(int argc, char ** argv)
> {
>   int result;
> 
>   /* Initialize Caml code */
>   caml_startup(argv);
>   /* Do some computation */
>   result = fib(10);
>   printf("fib(10) = %s\n", format_result(result));
>   return 0;
> }
> ----------------------------------------------------------
> 
> _________________________________________________________________
> Help STOP SPAM with the new MSN 8 and get 2 months FREE*  
> http://join.msn.com/?page=features/junkmail
> 
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


-- 
`No, you don't understand,' the Knight said, looking a little vexed. 
`That's what the name is called. The name really is "The Aged Aged 
Man."'
-- Lewis Carroll

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Caml-list] Interfacing C and Ocaml
  2003-06-19 19:58 [Caml-list] Interfacing C and Ocaml Jung Woon Ho
@ 2003-06-19 20:06 ` Kip Macy
  2003-06-19 20:28 ` art yerkes
  1 sibling, 0 replies; 16+ messages in thread
From: Kip Macy @ 2003-06-19 20:06 UTC (permalink / raw)
  To: Jung Woon Ho; +Cc: caml-list

try sticking in -L<path to libcurses.so> for -lcurses

On Thu, 19 Jun 2003, Jung Woon Ho wrote:

> Hi, Im trying to implement a simple program that interfaces C and Ocaml.
> Here is what I do:
> 
>         ocamlc -custom -output-obj -o modcaml.o mod.ml
>         ocamlc -c modwrap.c
>         cp /usr/local/lib/ocaml/libcamlrun.a mod.a
>         ar r mod.a modcaml.o modwrap.o
> 	cc -o prog main.c mod.a -lcurses
> 
> and I get the error:
> /usr/lib/gcc-lib/i486-suse-linux/3.2/../../../../i486-suse-linux/bin/ld: 
> cannot find -lcurses
> collect2: ld returned 1 exit status
> 
> I tried cc -o prog main.c mod.a -lcurses -ltermcap and cc -o prog main.c 
> mod.a -ltermcap but i cant get the program to link.
> 
> Does anybody know how to solve this problem?
> 
> Thank you very much.
> 
> 
> 
> 
> ---------------------mod.ml-------------------------------
> (* File mod.ml -- some ``useful'' Caml functions *)
> 
> let rec fib n = if n < 2 then 1 else fib(n-1) + fib(n-2)
> 
> let format_result n = Printf.sprintf "Result is: %d\n" n
> 
> (* Export those two functions to C *)
> 
> let _ = Callback.register "fib" fib
> let _ = Callback.register "format_result" format_result
> ----------------------------------------------------------
> 
> --------------------modwrap.c-----------------------------
> /* File modwrap.c -- wrappers around the Caml functions */
> 
> #include <stdio.h>
> #include <string.h>
> #include <caml/mlvalues.h>
> #include <caml/callback.h>
> 
> int fib(int n)
> {
>   static value * fib_closure = NULL;
>   if (fib_closure == NULL) fib_closure = caml_named_value("fib");
>   return Int_val(callback(*fib_closure, Val_int(n)));
> }
> 
> char * format_result(int n)
> {
>   static value * format_result_closure = NULL;
>   if (format_result_closure == NULL)
>     format_result_closure = caml_named_value("format_result");
>   return strdup(String_val(callback(*format_result_closure, Val_int(n))));
>   /* We copy the C string returned by String_val to the C heap
>      so that it remains valid after garbage collection. */
> }
> ----------------------------------------------------------
> 
> -------------------main.c---------------------------------
> /* File main.c -- a sample client for the Caml functions */
> 
> #include <stdio.h>
> 
> int main(int argc, char ** argv)
> {
>   int result;
> 
>   /* Initialize Caml code */
>   caml_startup(argv);
>   /* Do some computation */
>   result = fib(10);
>   printf("fib(10) = %s\n", format_result(result));
>   return 0;
> }
> ----------------------------------------------------------
> 
> _________________________________________________________________
> Help STOP SPAM with the new MSN 8 and get 2 months FREE*  
> http://join.msn.com/?page=features/junkmail
> 
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> 

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 16+ messages in thread

* [Caml-list] Interfacing C and Ocaml
@ 2003-06-19 19:58 Jung Woon Ho
  2003-06-19 20:06 ` Kip Macy
  2003-06-19 20:28 ` art yerkes
  0 siblings, 2 replies; 16+ messages in thread
From: Jung Woon Ho @ 2003-06-19 19:58 UTC (permalink / raw)
  To: caml-list; +Cc: unosoft

Hi, Im trying to implement a simple program that interfaces C and Ocaml.
Here is what I do:

        ocamlc -custom -output-obj -o modcaml.o mod.ml
        ocamlc -c modwrap.c
        cp /usr/local/lib/ocaml/libcamlrun.a mod.a
        ar r mod.a modcaml.o modwrap.o
	cc -o prog main.c mod.a -lcurses

and I get the error:
/usr/lib/gcc-lib/i486-suse-linux/3.2/../../../../i486-suse-linux/bin/ld: 
cannot find -lcurses
collect2: ld returned 1 exit status

I tried cc -o prog main.c mod.a -lcurses -ltermcap and cc -o prog main.c 
mod.a -ltermcap but i cant get the program to link.

Does anybody know how to solve this problem?

Thank you very much.




---------------------mod.ml-------------------------------
(* File mod.ml -- some ``useful'' Caml functions *)

let rec fib n = if n < 2 then 1 else fib(n-1) + fib(n-2)

let format_result n = Printf.sprintf "Result is: %d\n" n

(* Export those two functions to C *)

let _ = Callback.register "fib" fib
let _ = Callback.register "format_result" format_result
----------------------------------------------------------

--------------------modwrap.c-----------------------------
/* File modwrap.c -- wrappers around the Caml functions */

#include <stdio.h>
#include <string.h>
#include <caml/mlvalues.h>
#include <caml/callback.h>

int fib(int n)
{
  static value * fib_closure = NULL;
  if (fib_closure == NULL) fib_closure = caml_named_value("fib");
  return Int_val(callback(*fib_closure, Val_int(n)));
}

char * format_result(int n)
{
  static value * format_result_closure = NULL;
  if (format_result_closure == NULL)
    format_result_closure = caml_named_value("format_result");
  return strdup(String_val(callback(*format_result_closure, Val_int(n))));
  /* We copy the C string returned by String_val to the C heap
     so that it remains valid after garbage collection. */
}
----------------------------------------------------------

-------------------main.c---------------------------------
/* File main.c -- a sample client for the Caml functions */

#include <stdio.h>

int main(int argc, char ** argv)
{
  int result;

  /* Initialize Caml code */
  caml_startup(argv);
  /* Do some computation */
  result = fib(10);
  printf("fib(10) = %s\n", format_result(result));
  return 0;
}
----------------------------------------------------------

_________________________________________________________________
Help STOP SPAM with the new MSN 8 and get 2 months FREE*  
http://join.msn.com/?page=features/junkmail

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Caml-list] Interfacing C++ and Ocaml
  2001-03-30  9:13 ` Bruce Hoult
@ 2001-03-30  9:24   ` Fergus Henderson
  0 siblings, 0 replies; 16+ messages in thread
From: Fergus Henderson @ 2001-03-30  9:24 UTC (permalink / raw)
  To: Bruce Hoult; +Cc: David Chemouil, caml-list

On 30-Mar-2001, Bruce Hoult <bruce@hoult.org> wrote:
> I'm no expert on Caml, so maybe the wizards have done something 
> clever, but interfacing other languages to C++ is normally best done 
> by writing an adaptor in C++ that provides a pure C interface to the 
> C++ code (usable by C code, compiled by a C compiler, not C++), and 

In particular defining the C++ functions in your adaptor code
as `extern "C"' so that they can be called from C code.

-- 
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [Caml-list] Interfacing C++ and Ocaml
  2001-03-30  8:54 [Caml-list] Interfacing C++ " David Chemouil
@ 2001-03-30  9:13 ` Bruce Hoult
  2001-03-30  9:24   ` Fergus Henderson
  0 siblings, 1 reply; 16+ messages in thread
From: Bruce Hoult @ 2001-03-30  9:13 UTC (permalink / raw)
  To: David Chemouil, caml-list

At 10:54 AM +0200 3/30/01, David Chemouil wrote:
>Hi,
>
>
>
>I am in the situation where I would like to write an Ocaml program which
>makes use of some C++ classes. It seems to me some people had said there
>were some problems doing so, but I can't recall what these problems may
>be, and didn't find anything precise in the ML archive.
>
>So, I would like to know what may prevent me to reuse C++ code from
>Ocaml, if anything may. I am sure some arrangements can be done, as
>there exists an Ocaml implementation on BeOS, which is programmed in
>C++.

I'm no expert on Caml, so maybe the wizards have done something 
clever, but interfacing other languages to C++ is normally best done 
by writing an adaptor in C++ that provides a pure C interface to the 
C++ code (usable by C code, compiled by a C compiler, not C++), and 
then call *that* from your favourite language.

-- Bruce
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 16+ messages in thread

* [Caml-list] Interfacing C++ and Ocaml
@ 2001-03-30  8:54 David Chemouil
  2001-03-30  9:13 ` Bruce Hoult
  0 siblings, 1 reply; 16+ messages in thread
From: David Chemouil @ 2001-03-30  8:54 UTC (permalink / raw)
  To: caml-list


Hi,



I am in the situation where I would like to write an Ocaml program which
makes use of some C++ classes. It seems to me some people had said there
were some problems doing so, but I can't recall what these problems may
be, and didn't find anything precise in the ML archive.

So, I would like to know what may prevent me to reuse C++ code from
Ocaml, if anything may. I am sure some arrangements can be done, as
there exists an Ocaml implementation on BeOS, which is programmed in
C++. 

Moreover, if it is possible, what do you advise me to do? What are the
limitations? Have you got simple examples of such interfacings?


Thanks in advance,

dc



PS: BTW, I would like to thank these happy volunteers who aim at
packaging a Caml Development Kit. IMO, this is truly a good idea, which
may help Caml spread. Just a question to the Caml team: I would like to
know if (when?) you intend to include polymorphic I/O and some kind of
overloading in a forthcoming release of Caml. It seems to me Pierre had
written some mails about this, but is is now a long time ago, how is the
situation today?


-- 
David Chemouil    [mailto:David.Chemouil@irit.fr]

Institut de recherche en informatique de Toulouse
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 16+ messages in thread

* [Caml-list] Interfacing C++ and Ocaml
@ 2001-03-28  8:37 David Chemouil
  0 siblings, 0 replies; 16+ messages in thread
From: David Chemouil @ 2001-03-28  8:37 UTC (permalink / raw)
  To: caml-list


Hi,



I am in the situation where I would like to write an Ocaml program which
makes use of some C++ classes. It seems to me some people had said there
were some problems doing so, but I can't recall what these problems may
be, and didn't find anything precise in the ML archive.

So, I would like to know what may prevent me to reuse C++ code from
Ocaml, if anything may. I am sure some arrangements can be done, as
there exists an Ocaml implementation on BeOS, which is programmed in
C++. 

Moreover, if it is possible, what do you advise me to do? What are the
limitations? Have you got simple examples of such interfacings?


Thanks in advance,

dc



PS: BTW, I would like to thank these happy volunteers who aim at
packaging a Caml Development Kit. IMO, this is truly a good idea, which
may help Caml spread. Just a question to the Caml team: I would like to
know if you intend to include polymorphic I/O and some kind of
overloading in a forthcoming release of Caml. It seems to me Pierre had
written some mails about this, but is is now a long time ago, how is the
situation today?


-- 
David Chemouil    [mailto:David.Chemouil@irit.fr]

Institut de recherche en informatique de Toulouse
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2003-06-27 17:41 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-06-26 14:42 [Caml-list] interfacing C and OCaml Jung Woon Ho
2003-06-26 15:14 ` Lex Stein
2003-06-26 19:02 ` Jean-Baptiste Rouquier
2003-06-27  5:24   ` Re[2]: " Mikhail Fedotov
2003-06-27  8:31     ` Jean-Baptiste Rouquier
2003-06-27  8:59       ` Re[2]: " Mikhail Fedotov
2003-06-27 12:57         ` art yerkes
2003-06-27 13:14           ` Re[4]: " Mikhail Fedotov
2003-06-27 17:45             ` art yerkes
  -- strict thread matches above, loose matches on Subject: below --
2003-06-19 19:58 [Caml-list] Interfacing C and Ocaml Jung Woon Ho
2003-06-19 20:06 ` Kip Macy
2003-06-19 20:28 ` art yerkes
2001-03-30  8:54 [Caml-list] Interfacing C++ " David Chemouil
2001-03-30  9:13 ` Bruce Hoult
2001-03-30  9:24   ` Fergus Henderson
2001-03-28  8:37 David Chemouil

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).