caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* callbacks from C to Caml
@ 2006-01-31 11:09 Florent Monnier
  2006-01-31 11:12 ` [Caml-list] " Michael Wohlwend
  0 siblings, 1 reply; 4+ messages in thread
From: Florent Monnier @ 2006-01-31 11:09 UTC (permalink / raw)
  To: caml-list

Hi,

I'm trying to compile the simple exemple given here:
http://caml.inria.fr/pub/docs/manual-ocaml/manual032.html#htoc235
with the compilation instructions given there:
http://caml.inria.fr/pub/docs/manual-ocaml/manual032.html#htoc238
which are:
ocamlopt -output-obj -o camlcode.o unix.cmxa other .cmx and .cmxa files
cc -o myprog C objects and libraries \
        camlcode.o -L/usr/local/lib/ocaml -lunix -lasmrun

I have copy-past the OCaml code in test.ml and the C code in main.c
I use a standard GNU/Linux box [Mandriva 2006], with gcc 4.0.1
and OCaml 3.09.0

First I got errors like this:
undefined reference to `sqrt'
so I added -lm to gcc

Then those kind of errors:
undefined reference to `dlopen'
adding -ldl seems to be the solution.

Then new errors like this:
error: caml/mlvalues.h: No such file or directory
it seems that adding this fixes: -I'/usr/local/lib/ocaml'


Then the compilation is made with this:

% ocamlopt -c  test.ml
% ocamlopt -output-obj -o camlcode.o  unix.cmxa  test.cmx
% gcc main.c  -o myprog.opt  camlcode.o \
        -I'/usr/local/lib/ocaml' \
        -L/usr/local/lib/ocaml \
        -lunix -lasmrun  -lm -ldl

Now the compilation is made without any error messages, but:

% ./myprog.opt
Segmentation Error

What is wrong?
Where is the error?
What should I change/correct?


-- 
Thanks in advance
________________
% cat test.ml

let f n =
  print_string "f is applied to ";
  print_int n;
  print_newline();

let _ =
  Callback.register "test function" f;


% cat main.c
#include <stdio.h>
#include <stdlib.h>
#include <caml/mlvalues.h>
#include <caml/callback.h>

void call_caml_f(int arg)
{
    static value * closure_f = NULL;
    if (closure_f == NULL) {
        /* First time around, look up by name */
        closure_f = caml_named_value("test function");
    }
    caml_callback(*closure_f, Val_int(arg));
}

int main(void)
{
    call_caml_f(2);
    call_caml_f(5);

    return 0;
}


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

end of thread, other threads:[~2006-01-31 12:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-01-31 11:09 callbacks from C to Caml Florent Monnier
2006-01-31 11:12 ` [Caml-list] " Michael Wohlwend
2006-01-31 11:47   ` Florent Monnier
2006-01-31 12:03     ` Michael Wohlwend

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).