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 mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 0C577BBAF for ; Tue, 29 Jun 2010 13:18:59 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: At8BAEdzKUxRZ90vkWdsb2JhbACfOxUBAQEBCQsKBxEDH754hSQE X-IronPort-AV: E=Sophos;i="4.53,503,1272837600"; d="scan'208";a="53335971" Received: from mtaout01-winn.ispmail.ntl.com ([81.103.221.47]) by mail3-smtp-sop.national.inria.fr with ESMTP; 29 Jun 2010 13:18:58 +0200 Received: from aamtaout03-winn.ispmail.ntl.com ([81.103.221.35]) by mtaout01-winn.ispmail.ntl.com (InterMail vM.7.08.04.00 201-2186-134-20080326) with ESMTP id <20100629111858.XZAY3266.mtaout01-winn.ispmail.ntl.com@aamtaout03-winn.ispmail.ntl.com>; Tue, 29 Jun 2010 12:18:58 +0100 Received: from romulus.metastack.com ([81.102.132.77]) by aamtaout03-winn.ispmail.ntl.com (InterMail vG.2.02.00.01 201-2161-120-102-20060912) with ESMTP id <20100629111831.ENUA1598.aamtaout03-winn.ispmail.ntl.com@romulus.metastack.com>; Tue, 29 Jun 2010 12:18:31 +0100 Received: from remus.metastack.local ([172.16.0.1]) by romulus.metastack.com (8.14.2/8.14.2) with ESMTP id o5TBISXd032464 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=FAIL); Tue, 29 Jun 2010 12:18:29 +0100 Received: from Remus.metastack.local ([fe80::547c:3c42:e1da:eda2]) by Remus.metastack.local ([fe80::547c:3c42:e1da:eda2%11]) with mapi; Tue, 29 Jun 2010 12:18:26 +0100 From: David Allsopp To: "'Andreas Sommer'" , "'caml-list@yquem.inria.fr'" Subject: RE: [Caml-list] Calling OCaml from C - nothing shown on stdout Thread-Topic: [Caml-list] Calling OCaml from C - nothing shown on stdout Thread-Index: AQHLF3gWuthqdxjvWUqmEtWe922FZpKYxafQ Date: Tue, 29 Jun 2010 11:18:25 +0000 Message-ID: References: <4C29CE90.60708@web.de> In-Reply-To: <4C29CE90.60708@web.de> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Organization: MetaStack Solutions Ltd. X-Scanned-By: MIMEDefang 2.65 on 81.102.132.77 X-Cloudmark-Analysis: v=1.1 cv=ZtHxNT4mZm3rCuM0SmWmgWxeBwJsziC8EqOrwwVkrhA= c=1 sm=0 a=OU2TSCJ3H3gA:10 a=yQWWgrYGNuUA:10 a=8nJEP1OIZ-IA:10 a=5q2cPjp4b7_ku1MePiEA:9 a=i8e1qXvlYdS0c9rI_qVYDjvDkBwA:4 a=wPNLvfGTeEIA:10 a=HpAAvcLHHh0Zw7uRqdWCyQ==:117 X-Spam: no; 0.00; ocaml:01 stdout:01 ocaml:01 printf:01 printf:01 fprintf:01 stdout:01 fprintf:01 runtime:01 runtime:01 pervasives:01 kprintf:01 kprintf:01 shorthand:98 wrote:01 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 () =3D >=A0=A0=A0 Printf.fprintf stdout "%s" "test" >=A0=A0=A0=20 > let () =3D >=A0=A0=A0 let oc =3D open_out "/tmp/testfile" in >=A0=A0=A0 Printf.fprintf oc "test"; >=A0=A0=A0 close_out oc; >=A0=A0=A0=20 >=A0=A0=A0 Printf.fprintf stdout "Caml main function\n"; >=A0=A0=A0=20 >=A0=A0=A0 Callback.register "Hello callback" hello; >=A0=A0=A0=20 >=A0=A0=A0 Printf.fprintf stdout "%s" "Finished with OCaml initialization" >;; A couple of observations on your coding style - you open Printf and then pr= oceed to use qualified Printf.fprintf calls. There's no need for the open s= tatement. Printf.printf is a shorthand for "Printf.fprintf stdout" which ma= kes 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: As far as I can tell from the manual, there's no particular difference in n= ative code between using caml_main and caml_startup. Your problem is the bu= ffering of stdout in the OCaml runtime. There are two ways of working aroun= d 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 tha= t...). For example, if I added [; exit 0] to the end of your hello ML funct= ion then I get the lines from the Printf calls on the ML side, but out of o= rder (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, c= reate another function using Printf.kprintf which flushes stdout each time: let flushed_printf x =3D Printf.kprintf (fun s -> print_string s; flush= stdout) in flushed_printf "%s" "Finished with..." David