caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Calling OCaml from C - nothing shown on stdout
@ 2010-06-29 10:44 Andreas Sommer
  2010-06-29 10:52 ` [Caml-list] " Erik de Castro Lopo
  2010-06-29 11:18 ` David Allsopp
  0 siblings, 2 replies; 5+ messages in thread
From: Andreas Sommer @ 2010-06-29 10:44 UTC (permalink / raw)
  To: caml-list

[-- 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 --]

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

end of thread, other threads:[~2010-06-29 12:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-06-29 10:44 Calling OCaml from C - nothing shown on stdout Andreas Sommer
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

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