From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 662D0820A1 for ; Tue, 10 Sep 2013 15:22:05 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of info@gerd-stolpmann.de) identity=pra; client-ip=212.227.17.9; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="info@gerd-stolpmann.de"; x-sender="info@gerd-stolpmann.de"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of info@gerd-stolpmann.de) identity=mailfrom; client-ip=212.227.17.9; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="info@gerd-stolpmann.de"; x-sender="info@gerd-stolpmann.de"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of postmaster@moutng.kundenserver.de designates 212.227.17.9 as permitted sender) identity=helo; client-ip=212.227.17.9; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="info@gerd-stolpmann.de"; x-sender="postmaster@moutng.kundenserver.de"; x-conformance=sidf_compatible; x-record-type="v=spf1" X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgIBAF8cL1LU4xEJk2dsb2JhbABbgz+De78RgSMWDgEBAQEHCwsJFAMlgiUBAQUjMiQQCw4KKgICVwYTCYd9CK1qkhSJNYYAJgeCaYE0A454hSSFCYUAjl4 X-IPAS-Result: AgIBAF8cL1LU4xEJk2dsb2JhbABbgz+De78RgSMWDgEBAQEHCwsJFAMlgiUBAQUjMiQQCw4KKgICVwYTCYd9CK1qkhSJNYYAJgeCaYE0A454hSSFCYUAjl4 X-IronPort-AV: E=Sophos;i="4.90,878,1371074400"; d="asc'?scan'208";a="26360769" Received: from moutng.kundenserver.de ([212.227.17.9]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 10 Sep 2013 15:22:04 +0200 Received: from office1.lan.sumadev.de (dslb-088-069-140-232.pools.arcor-ip.net [88.69.140.232]) by mrelayeu.kundenserver.de (node=mrbap0) with ESMTP (Nemesis) id 0M1zLv-1W7M9H237T-00tk5r; Tue, 10 Sep 2013 15:21:56 +0200 Received: from [192.168.0.110] (ip-109-90-191-98.unitymediagroup.de [109.90.191.98]) by office1.lan.sumadev.de (Postfix) with ESMTPSA id 126B6C00CF; Tue, 10 Sep 2013 15:21:56 +0200 (CEST) Message-ID: <1378819315.10274.148.camel@zotac> From: Gerd Stolpmann To: David Allsopp Cc: caml-list Date: Tue, 10 Sep 2013 15:21:55 +0200 In-Reply-To: References: Content-Type: multipart/signed; micalg="pgp-sha1"; protocol="application/pgp-signature"; boundary="=-C3njXEmaTJqFEnHE6D7h" X-Mailer: Evolution 3.2.3-0ubuntu6 Mime-Version: 1.0 X-Provags-ID: V02:K0:S1L+Yle4RE++pONlBj+JWP0RF5boC9reMKa1dy/n+Oa r12wvQnVqboNPVEr8P8RuR6m3OPUkv4cbr2VepavMgHOzSmgKW LOX0+xX9ZUoaITaMw5auTIK/fF6HD8eAcMgpc8AZNafK8pG1S4 Jj6DrCGIUhhTqrnqdxx4V5AE8YgxrSEDyhLfmsWku+Yf8j3+85 fHC1wNW1iPmPYUaGRPCOClMhtIDizn58vQfoCl+m9MfbVdiMzT WrwSeUcZfmd/W9a4LYYReW67LMC0gAw4LGDsRWCWDXV8/3oWUV XnS2fzjqgUhNqbWiOTEEvZmhXyEFSXIzzG8kZZopZUwLzSesL0 MHU7QTQLGtsMtB+AvI8zEeez7PZS7vZyJ/GCOibjj Subject: RE: [Caml-list] Using OCaml from C --=-C3njXEmaTJqFEnHE6D7h Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Am Dienstag, den 10.09.2013, 12:41 +0000 schrieb David Allsopp: > 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 (i= n 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). >=20 > The documentation is arguably deficient here - you can get those two extr= a switches from the output of ocamlc -config (cf bytecomp_c_libraries and n= ative_c_libraries) >=20 > > 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 -lun= ix > >=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 -lun= ix > > -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: >=20 > General tip for these: ocamlobjinfo library.cm[x]a will include in its ou= tput the required additional linking switches will save a few of these... >=20 > Adding -verbose (or using ocamlfind query) will allow you to locate the a= ppropriate .cma or .cmxa for the findlib package in question. >=20 > I don't *think* that ocamlfind itself can help you with compilation in th= is direction (i.e. compiling OCaml to be used in C). No, it cannot help here, because it cannot look inside the cma's to find out the linker flags. What I recommend here is to create an empty dummy.ml module, and do ocamlfind ocamlc -verbose -package core,str -linkpkg -linkall -thread -custom which will print the final cc command ocamlc uses itself to link the program. You can even intercept this command if you also pass -cc ./my_own_script so that this script is called instead of cc. Just keep all the -L and -l switches (and select other ones like -pthread). The order of the -l switches matters: if you pass -lfoo -lbar to the linker, library foo can use bar but not the other way round (note that this is the opposite order you are used from ocaml). > > /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 >=20 > 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 replaceme= nt so I wouldn't expect to be allowed to use both standard library and core= modules in the same program. If you are supposed to be able to do, then th= at sounds like a possible bug in core... Looks like Core uses the same symbols as Unix. IMHO a bug in Core. Of course you can use Core with other libraries (which will in turn use the normal Unix module), because it's not a stdlib replacement, and you have to open Core before you can see its definitions. Gerd >=20 > > -- > >=20 > > My real question is: what command *should* I be using to compile my > > example? Or is the above more-or-less expected? >=20 > They look fine - I hope that the ocamlobjinfo tip allows for less trial-a= nd-error in the future! >=20 >=20 > David=20 >=20 --=20 ------------------------------------------------------------ Gerd Stolpmann, Darmstadt, Germany gerd@gerd-stolpmann.de My OCaml site: http://www.camlcity.org Contact details: http://www.camlcity.org/contact.html Company homepage: http://www.gerd-stolpmann.de ------------------------------------------------------------ --=-C3njXEmaTJqFEnHE6D7h Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iQEcBAABAgAGBQJSLxzzAAoJEAaM4b9ZLB5T024H/jfAdHr5LJ8YqGhWa3DZ5hS9 7brwptA74o3/8akQy1FI3z1FIjVRYCMLBFYpBuTi+BeUCLKJMUrjj1ufTcnOitmJ LAP29jCgM/a5m3iyQ45tS/Wn9QuNqhkGzSAYzxJ0qzrtsQu41sHwLljgrtzhAu3O 77jjXR9kItpZd+SvCtov4raaI1INnM8dP7LGY5/SsVxcrVQcWueNrmk8+GF7GZcw ipasF1NRM5rfY1Hj06t7IsSt1r0ysTJwAvCItcGY5RcjdVRbq/rDEw81vTUqfSho gDWhLx6975IBmeuil87c+zqPAPTeeALNER+eclIbAiiuNTCaPLFYkRobrU+VROo= =PpYK -----END PGP SIGNATURE----- --=-C3njXEmaTJqFEnHE6D7h--