From: David Allsopp <dra-news@metastack.com>
To: "'Andreas Sommer'" <AndiDog@web.de>,
"'caml-list@yquem.inria.fr'" <caml-list@yquem.inria.fr>
Subject: RE: [Caml-list] Calling OCaml from C - nothing shown on stdout
Date: Tue, 29 Jun 2010 11:18:25 +0000 [thread overview]
Message-ID: <E51C5B015DBD1348A1D85763337FB6D908F27E@Remus.metastack.local> (raw)
In-Reply-To: <4C29CE90.60708@web.de>
Andreas Sommer wrote:
> 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"
>;;
A couple of observations on your coding style - you open Printf and then proceed to use qualified Printf.fprintf calls. There's no need for the open statement. Printf.printf is a shorthand for "Printf.fprintf stdout" which makes the code a bit clearer if you're skimming through it (as Printf.fprintf at a glance looks like actual file I/O rather than channel I/O).
> and this is the C file:
<snip>
As far as I can tell from the manual, there's no particular difference in native code between using caml_main and caml_startup. Your problem is the buffering of stdout in the OCaml runtime. There are two ways of working around this:
1. Allow the runtime to exit using Pervasives.exit at some point on the ML side (or call caml_sys_exit directly in your C code but I haven't tried that...). For example, if I added [; exit 0] to the end of your hello ML function then I get the lines from the Printf calls on the ML side, but out of order (all of the OCaml ones come at the end after the C ones)
2. Insert a flush stdout statement after each Printf.printf call. Better, create another function using Printf.kprintf which flushes stdout each time:
let flushed_printf x = Printf.kprintf (fun s -> print_string s; flush stdout) in
flushed_printf "%s" "Finished with..."
David
next prev parent reply other threads:[~2010-06-29 11:18 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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=E51C5B015DBD1348A1D85763337FB6D908F27E@Remus.metastack.local \
--to=dra-news@metastack.com \
--cc=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