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 mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 08AA0BBAF for ; Sun, 15 Nov 2009 14:27:56 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApMCAOuQ/0quhHTij2dsb2JhbACcGwEBAQEJCwgctHqEPASBbQ X-IronPort-AV: E=Sophos;i="4.44,746,1249250400"; d="scan'208";a="40135125" Received: from birch.site5.com ([174.132.116.226]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 15 Nov 2009 14:27:55 +0100 Received: from pool-96-232-2-87.nycmny.east.verizon.net ([96.232.2.87] helo=[192.168.0.105]) by birch.site5.com with esmtp (Exim 4.69) (envelope-from ) id 1N9f8z-0002Lb-Ff for caml-list@yquem.inria.fr; Sun, 15 Nov 2009 07:27:53 -0600 Message-Id: From: John Nowak To: caml-list@yquem.inria.fr Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Mime-Version: 1.0 (Apple Message framework v936) Subject: calling OCaml from C on non-Mac platforms? Date: Sun, 15 Nov 2009 08:27:49 -0500 X-Mailer: Apple Mail (2.936) X-ACL-Warn: { X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - birch.site5.com X-AntiAbuse: Original Domain - yquem.inria.fr X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - johnnowak.com X-Source: X-Source-Args: X-Source-Dir: X-Spam: no; 0.00; ocaml:01 ocaml:01 mlvalues:01 val:01 argc:01 argv:01 argv:01 endline:01 ocamlopt:01 cmx:01 ocamlopt:01 -output-obj:01 cmx:01 gcc:01 ocamlc:01 I'm trying to call OCaml from C. Essentially, I have this for "main.c": #include #include void hello() { static value *closure = NULL; if(!closure) closure = caml_named_value("hello"); caml_callback(*closure, Val_unit); } int main(int argc, char **argv) { caml_main(argv); hello(); return 0; } And the following "hello.ml": let hello () = print_endline "Hello, world!" let () = Callback.register "hello" hello I can then compile as follows on OS X 10.5: ocamlopt -c hello.ml -o hello.cmx ocamlopt -output-obj hello.cmx -o hello.o gcc -c main.c -o main.o -I"`ocamlc -where`" gcc -o hello hello.o main.o -L"`ocamlc -where`" -ldl -lm -lasmrun This works fine on OS X. However, it does not seem to work on Linux/ AMD64 or FreeBSD/AMD64. I've tried suggestions from all over the web and I've tried the approach given in the manual and nothing works. In all non-Mac cases, I get this on the second line: hello.o: file not recognized: File truncated Error during linking One thing I can do is do this instead of the first two lines: ocamlopt -c -output-obj hello.ml However, then I get the following: hello.o: In function `camlHello__hello_58': (.text+0x8): undefined reference to `camlPervasives__print_endline_298' /usr/lib/ocaml/3.10.2/libasmrun.a(startup.o): In function `caml_main': (.text+0x25f): undefined reference to `caml_data_segments' /usr/lib/ocaml/3.10.2/libasmrun.a(startup.o): In function `caml_main': (.text+0x273): undefined reference to `caml_code_segments' ... Does anyone have a correct set of instructions for doing this on non- Mac platforms? Thanks. - John