* 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
* Re: [Caml-list] Calling OCaml from C - nothing shown on stdout
2010-06-29 10:44 Calling OCaml from C - nothing shown on stdout Andreas Sommer
@ 2010-06-29 10:52 ` Erik de Castro Lopo
2010-06-29 11:18 ` David Allsopp
1 sibling, 0 replies; 5+ messages in thread
From: Erik de Castro Lopo @ 2010-06-29 10:52 UTC (permalink / raw)
To: caml-list
Andreas Sommer wrote:
> Hi everybody,
>
> I'm trying to call a OCaml function from C code.
I don't see a call to caml_startup() from the C code which I believe
is necessary.
I've blogged this exact problem (calling into Ocaml from C) here:
http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/calling_ocaml.html
Cheers,
Erik
--
----------------------------------------------------------------------
Erik de Castro Lopo
http://www.mega-nerd.com/
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: [Caml-list] Calling OCaml from C - nothing shown on stdout
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
1 sibling, 2 replies; 5+ messages in thread
From: David Allsopp @ 2010-06-29 11:18 UTC (permalink / raw)
To: 'Andreas Sommer', 'caml-list@yquem.inria.fr'
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Calling OCaml from C - nothing shown on stdout
2010-06-29 11:18 ` David Allsopp
@ 2010-06-29 11:52 ` Matthieu Dubuget
2010-06-29 12:45 ` Andreas Sommer
1 sibling, 0 replies; 5+ messages in thread
From: Matthieu Dubuget @ 2010-06-29 11:52 UTC (permalink / raw)
To: caml-list
-------- Original Message --------
Subject: Re: [Caml-list] Calling OCaml from C - nothing shown on stdout
From: David Allsopp <dra-news@metastack.com>
To: 'Andreas Sommer' <AndiDog@web.de>, 'caml-list@yquem.inria.fr'
<caml-list@yquem.inria.fr>
Date: 29/06/2010 13:18
>
> 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..."
>
Or use "%!" in your string format to flush.
Salutations
Matt
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Calling OCaml from C - nothing shown on stdout
2010-06-29 11:18 ` David Allsopp
2010-06-29 11:52 ` Matthieu Dubuget
@ 2010-06-29 12:45 ` Andreas Sommer
1 sibling, 0 replies; 5+ messages in thread
From: Andreas Sommer @ 2010-06-29 12:45 UTC (permalink / raw)
To: David Allsopp; +Cc: caml-list
David Allsopp wrote:
>
>
> 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:
>
The caml_main and caml_startup functions are indeed identical
(caml_startup only calls caml_main). You were right about the stdout
buffering, and I will use your flushed_printf solution. Thanks for your
help!
Andreas
^ 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