Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Florent Monnier <fmonnier@linux-nantes.fr.eu.org>
To: caml-list@inria.fr
Subject: callbacks from C to Caml
Date: Tue, 31 Jan 2006 12:09:12 +0100	[thread overview]
Message-ID: <200601311209.13125.fmonnier@linux-nantes.fr.eu.org> (raw)

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;
}


             reply	other threads:[~2006-01-31 10:59 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-01-31 11:09 Florent Monnier [this message]
2006-01-31 11:12 ` [Caml-list] " Michael Wohlwend
2006-01-31 11:47   ` Florent Monnier
2006-01-31 12:03     ` Michael Wohlwend

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200601311209.13125.fmonnier@linux-nantes.fr.eu.org \
    --to=fmonnier@linux-nantes.fr.eu.org \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox