From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id DC72A820A1 for ; Tue, 10 Sep 2013 14:41:59 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of dra-news@metastack.com) identity=pra; client-ip=62.13.128.65; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="dra-news@metastack.com"; x-sender="dra-news@metastack.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of dra-news@metastack.com designates 62.13.128.65 as permitted sender) identity=mailfrom; client-ip=62.13.128.65; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="dra-news@metastack.com"; x-sender="dra-news@metastack.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@outmail128065.authsmtp.net) identity=helo; client-ip=62.13.128.65; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="dra-news@metastack.com"; x-sender="postmaster@outmail128065.authsmtp.net"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ArkBAAkTL1I+DYBBnGdsb2JhbABbgmZZUcI7gSYWDgEBAQEBCBQJPIIlAQEBAwE6MRMLAgEIGAoUEDIlAgQbh3QHAwi+ao8qOIMdgQADl3aBL5Ncgio X-IPAS-Result: ArkBAAkTL1I+DYBBnGdsb2JhbABbgmZZUcI7gSYWDgEBAQEBCBQJPIIlAQEBAwE6MRMLAgEIGAoUEDIlAgQbh3QHAwi+ao8qOIMdgQADl3aBL5Ncgio X-IronPort-AV: E=Sophos;i="4.90,878,1371074400"; d="scan'208";a="26354650" Received: from outmail128065.authsmtp.net ([62.13.128.65]) by mail3-smtp-sop.national.inria.fr with ESMTP; 10 Sep 2013 14:41:58 +0200 Received: from mail-c235.authsmtp.com (mail-c235.authsmtp.com [62.13.128.235]) by punt10.authsmtp.com (8.14.2/8.14.2) with ESMTP id r8ACfwUa079697 for ; Tue, 10 Sep 2013 13:41:58 +0100 (BST) Received: from romulus.metastack.com (cpc1-cmbg5-0-0-cust241.5-4.cable.virginmedia.com [81.98.252.242]) (authenticated bits=0) by mail.authsmtp.com (8.14.2/8.14.2/) with ESMTP id r8ACfuBO067050 for ; Tue, 10 Sep 2013 13:41:56 +0100 (BST) Received: from remus.metastack.local (remus.metastack.com [172.16.0.1]) by romulus.metastack.com (8.14.2/8.14.2) with ESMTP id r8ACftql016181 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=FAIL) for ; Tue, 10 Sep 2013 13:41:56 +0100 Received: from Remus.metastack.local ([fe80::547c:3c42:e1da:eda2]) by Remus.metastack.local ([fe80::547c:3c42:e1da:eda2%10]) with mapi id 14.03.0158.001; Tue, 10 Sep 2013 13:41:55 +0100 From: David Allsopp To: caml-list Thread-Topic: [Caml-list] Using OCaml from C Thread-Index: AQHOriBnwYEbm2JY6UyFsny4fGerIpm+5ssA Date: Tue, 10 Sep 2013 12:41:55 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [172.16.0.18] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Organization: MetaStack Solutions Ltd. X-Scanned-By: MIMEDefang 2.65 on 172.16.0.20 X-Server-Quench: 60be75c2-1a16-11e3-b802-002590a15da7 X-AuthReport-Spam: If SPAM / abuse - report it at: http://www.authsmtp.com/abuse X-AuthRoute: OCd1ZAARAlZZVg1f DC4bFwdFRBksPQFF ChxFJgxfNlEAUAAU NkdBMnJSNkcdTBdX QSgKW0s3BR1uW2N0 aBpQbg9ZY0BLWUti UVZASkxQEQd2AxgD GRwbTRk8NQ03Cyg1 FzliWHdbWUQ0cEF4 QgBSFT9TYmIzan0e AUFeagJVJQBXLBdF aE1/VHEIaGVWZ380 FlAlBR1jdQZ0ISFR BwUMNk4nCXsQHzgz DzcpFDArVUMfQCB7 BHQA X-Authentic-SMTP: 61633634383431.1023:706 X-AuthFastPath: 0 (Was 255) X-AuthSMTP-Origin: 81.98.252.242/25 X-AuthVirus-Status: No virus detected - but ensure you scan with your own anti-virus system. Subject: RE: [Caml-list] Using OCaml from C Tom Ridge wrote: > Dear List, >=20 > I want to use my ocaml code from a C program. >=20 > I have read the section "Interfacing C with OCaml" in the manual: >=20 > http://caml.inria.fr/pub/docs/manual-ocaml/manual033.html >=20 > The example in section 19.8 works fine. The compilation commands are (in a > Makefile; OCAMLPATH points to /home/tr61/.opam/4.00.1): >=20 > ocamlc -custom -output-obj -o modcaml.o mod.ml > ocamlc -c modwrap.c > cp $(OCAMLPATH)/lib/ocaml/libcamlrun.a mod.a > ar r mod.a modcaml.o modwrap.o > cc -o prog main.c mod.a -lcurses -lm -ldl >=20 > (I had to add -lm and -ldl to get the example to work). The documentation is arguably deficient here - you can get those two extra = switches from the output of ocamlc -config (cf bytecomp_c_libraries and nat= ive_c_libraries) > If my code makes use of the OCaml Unix library, I do: >=20 > cc -o prog main.c mod.a -lcurses -lm -ldl -L$(OCAMLPATH)/lib/ocaml -lunix >=20 > And if my code makes use of threads, I do: >=20 > cc -o prog main.c mod.a -lcurses -lm -ldl -L$(OCAMLPATH)/lib/ocaml -lunix > -lthreads -lpthread >=20 > So far, so good. The real code I want to use makes use of the Core > library. The compilation commands I finally got working are: >=20 > ocamlfind ocamlc -package core -package str -linkpkg -thread -custom - > output-obj -o modcaml.o pqueue.ml mod.ml > ocamlc -c modwrap.c > cp $(OCAMLPATH)/lib/ocaml/libcamlrun.a mod.a > ar r mod.a modcaml.o modwrap.o > cc -pthread -Xlinker --allow-multiple-definition -o prog main.c mod.a - > lcurses -lm -ldl -lpthread -L$(OCAMLPATH)/lib/ocaml -lunix -lthreads - > lbigarray -lcamlstr -lnums -L$(OCAMLPATH)/lib/core_kernel - > lcore_kernel_stubs -L$(OCAMLPATH)/lib/core -lcore_stubs - > L$(OCAMLPATH)/lib/bin_prot -lbin_prot_stubs -lrt >=20 > For the last "cc" command, tt took quite a long time to figure out all > these flags and options (I do not know very much about linkers etc). > The errors I got were: General tip for these: ocamlobjinfo library.cm[x]a will include in its outp= ut the required additional linking switches will save a few of these... Adding -verbose (or using ocamlfind query) will allow you to locate the app= ropriate .cma or .cmxa for the findlib package in question. I don't *think* that ocamlfind itself can help you with compilation in this= direction (i.e. compiling OCaml to be used in C). > -- >=20 > /home/tr61/.opam/4.00.1/lib/ocaml/libthreads.a(st_stubs_b.o): In function > `caml_thread_initialize': > st_stubs.c:(.text+0xed1): undefined reference to `pthread_atfork >=20 > fixed by adding -pthread after cc command >=20 > -- >=20 > /home/tr61/.opam/4.00.1/lib/core/libcore_stubs.a(unix_stubs.o): In > function `unix_clock_gettime': > unix_stubs.c:(.text+0x2776): undefined reference to `clock_gettime' >=20 > I had -lrt after -lpthread, but the error was still reported; when I moved > -lrt to end of command line things seemed to work; why? Linker options for libraries should always be last (see gcc docs). I expect= (but I'm sure a compiler expert can confirm/deny) that it's because the li= nker only pulls in symbols which it knows are needed (or at least it's allo= wed to do that - i.e. you need to specify foo.o requiring symbol bar before= specifying -lfoo). > -- >=20 > /home/tr61/.opam/4.00.1/lib/core/libcore_stubs.a(unix_stubs.o): In > function `unix_initgroups': > unix_stubs.c:(.text+0x3b90): multiple definition of `unix_initgroups' > /home/tr61/.opam/4.00.1/lib/ocaml/libunix.a(initgroups.o):initgroups.c:(.t > ext+0x0): > first defined here > /home/tr61/.opam/4.00.1/lib/core/libcore_stubs.a(unix_stubs.o): In > function `unix_nice': > unix_stubs.c:(.text+0x46bc): multiple definition of `unix_nice' > /home/tr61/.opam/4.00.1/lib/ocaml/libunix.a(nice.o):nice.c:(.text+0x0): > first defined here >=20 > fixed by adding -Xlinker --allow-multiple-definition to cc options Are you definitely allowed to use both Core and Unix in the same program (i= .e. can you link on OCaml program that way?). Core is an stdlib replacement= so I wouldn't expect to be allowed to use both standard library and core m= odules in the same program. If you are supposed to be able to do, then that= sounds like a possible bug in core... > -- >=20 > My real question is: what command *should* I be using to compile my > example? Or is the above more-or-less expected? They look fine - I hope that the ocamlobjinfo tip allows for less trial-and= -error in the future! David=20