caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Andreas Sommer <AndiDog@web.de>
To: caml-list@yquem.inria.fr
Subject: Calling OCaml from C - nothing shown on stdout
Date: Tue, 29 Jun 2010 12:44:32 +0200	[thread overview]
Message-ID: <4C29CE90.60708@web.de> (raw)

[-- Attachment #1: Type: text/plain, Size: 1755 bytes --]

Hi everybody,

I'm trying to call a OCaml function from C code. This is the OCaml file:

    open Printf

    let hello () =
        Printf.fprintf stdout "%s" "test"
       
    let () =
        let oc = open_out "/tmp/testfile" in
        Printf.fprintf oc "test";
        close_out oc;
       
        Printf.fprintf stdout "Caml main function\n";
       
        Callback.register "Hello callback" hello;
       
        Printf.fprintf stdout "%s" "Finished with OCaml initialization"
    ;;


and this is the C file:

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

    void helloWrapper()
    {
        static value *closure_f = NULL;

        if(closure_f == NULL)
        {
            printf("1\n");
            closure_f = caml_named_value("Hello callback");
            printf("2\n");
            printf("Closure pointer: %p\n", closure_f);
        }
        printf("3\n");
        caml_callback(*closure_f, Val_unit);
    }

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

        printf("calling wrapper\n");
        helloWrapper();

        printf("Done.");
        return 0;
    }


Everything seems to work: The file "/tmp/testfile" is created correctly
(so the OCaml function did get called), and the C program ends with
"Done.", *but* nothing is printed from the OCaml fprintf functions.

I am linking the executable with

    $(OCAMLOPT) -output-obj test.ml -o test_obj.o
    $(CC) -c linkwiththis.c -o linkwiththis.o -I"`$(OCAMLC) -where`"
    $(CC) linkwiththis.o test_obj.o $(NATIVECCLIBS) -lasmrun -o
    camltestprogram -L"`$(OCAMLC) -where`"


What am I doing wrong - how can I make OCaml print to stdout correctly?

Best regards,
    Andreas

[-- Attachment #2: Type: text/html, Size: 2634 bytes --]

             reply	other threads:[~2010-06-29 10:44 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-29 10:44 Andreas Sommer [this message]
2010-06-29 10:52 ` [Caml-list] " Erik de Castro Lopo
2010-06-29 11:18 ` David Allsopp
2010-06-29 11:52   ` Matthieu Dubuget
2010-06-29 12:45   ` Andreas Sommer

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=4C29CE90.60708@web.de \
    --to=andidog@web.de \
    --cc=caml-list@yquem.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;
as well as URLs for NNTP newsgroup(s).