From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id C29F8BBAF for ; Tue, 29 Jun 2010 12:44:33 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AiMCAFlrKUzZSMDjkWdsb2JhbACTBYw2FQEBAQEJCwoHEQMfvmSFJAQ X-IronPort-AV: E=Sophos;i="4.53,503,1272837600"; d="scan'208,217";a="54153396" Received: from fmmailgate02.web.de ([217.72.192.227]) by mail2-smtp-roc.national.inria.fr with ESMTP; 29 Jun 2010 12:44:33 +0200 Received: from smtp07.web.de ( [172.20.5.215]) by fmmailgate02.web.de (Postfix) with ESMTP id E65501699AB35 for ; Tue, 29 Jun 2010 12:44:32 +0200 (CEST) Received: from [91.47.104.188] (helo=[192.168.2.100]) by smtp07.web.de with asmtp (TLSv1:AES256-SHA:256) (WEB.DE 4.110 #4) id 1OTYIq-0000l5-00 for caml-list@yquem.inria.fr; Tue, 29 Jun 2010 12:44:32 +0200 Message-ID: <4C29CE90.60708@web.de> Date: Tue, 29 Jun 2010 12:44:32 +0200 From: Andreas Sommer User-Agent: Thunderbird 2.0.0.24 (Windows/20100228) MIME-Version: 1.0 To: caml-list@yquem.inria.fr Subject: Calling OCaml from C - nothing shown on stdout X-Enigmail-Version: 0.96.0 Content-Type: multipart/alternative; boundary="------------070700030509090207050706" Sender: AndiDog@web.de X-Sender: AndiDog@web.de X-Provags-ID: V01U2FsdGVkX1+kU2fUGkeNJu/Jo1wClZkgveIAYoTnJjoNY9UF DCcDiijIaXyG4XferqSgz9ydT22x4nWg/3ezKVRsIfd1cvK2l+ EB5iELtnA= X-Spam: no; 0.00; ocaml:01 stdout:01 ocaml:01 printf:01 printf:01 fprintf:01 stdout:01 fprintf:01 stdio:01 mlvalues:01 pointer:01 val:01 argc:01 argv:01 argv:01 This is a multi-part message in MIME format. --------------070700030509090207050706 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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 #include #include #include 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 --------------070700030509090207050706 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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
--------------070700030509090207050706--