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 84110820A1 for ; Tue, 10 Sep 2013 14:22:04 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of tom.j.ridge@googlemail.com) identity=pra; client-ip=209.85.192.172; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="tom.j.ridge@googlemail.com"; x-sender="tom.j.ridge@googlemail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of tom.j.ridge@googlemail.com designates 209.85.192.172 as permitted sender) identity=mailfrom; client-ip=209.85.192.172; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="tom.j.ridge@googlemail.com"; x-sender="tom.j.ridge@googlemail.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@mail-pd0-f172.google.com) identity=helo; client-ip=209.85.192.172; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="tom.j.ridge@googlemail.com"; x-sender="postmaster@mail-pd0-f172.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AswBAEIOL1LRVcCsm2dsb2JhbABbgz9Rw1gIFg4BAQEBAQYLCwkUKIJsAQE4EBVdEgEFASKIAgEDDwygQYsMhEoBBYQ4ChknDYkpBpN/l3mBL45TGCmBYYJoOw X-IPAS-Result: AswBAEIOL1LRVcCsm2dsb2JhbABbgz9Rw1gIFg4BAQEBAQYLCwkUKIJsAQE4EBVdEgEFASKIAgEDDwygQYsMhEoBBYQ4ChknDYkpBpN/l3mBL45TGCmBYYJoOw X-IronPort-AV: E=Sophos;i="4.90,878,1371074400"; d="scan'208";a="26351666" Received: from mail-pd0-f172.google.com ([209.85.192.172]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 10 Sep 2013 14:22:03 +0200 Received: by mail-pd0-f172.google.com with SMTP id z10so7577031pdj.31 for ; Tue, 10 Sep 2013 05:22:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=mime-version:sender:from:date:message-id:subject:to:content-type; bh=Fhn1M6KzVBs4GDWPngR1bftZ8n9GvefDFgSh4RFWv2s=; b=P6FX8mUl7TPwm7+Nz5zmtwiRjwHYA4LbHmbPCc7eWw5oFCPs2mrQAOSeSMiZJ37Nl+ 1JcGkc+UVXWKCfWasyG+gHngDanglxrpExTQsCcP1Bm2Wv/UtpXqqa13mRxwQDCumhiS X0qVwuyqi0qbwjRA846hB06ntMatr37HxFWnk4R9ql/BicBC/GjVTH07LL6QfS1UUCmw YgtH6t8kSFdaOJ7EBNa5YpF7gZxFNlL4UPYL2eaW2Al8Ksx6lI1fwcPIGDsWs0uCEm1Q eBDF6hWzEvaKym0djS3u0UQqWCSi6ud39PlGaL1ydF38R71a4hpRhcg3vJ/0KjDapi21 t1ww== X-Received: by 10.66.119.202 with SMTP id kw10mr14339737pab.118.1378815721912; Tue, 10 Sep 2013 05:22:01 -0700 (PDT) MIME-Version: 1.0 Sender: tom.j.ridge@googlemail.com Received: by 10.70.79.136 with HTTP; Tue, 10 Sep 2013 05:21:41 -0700 (PDT) From: Tom Ridge Date: Tue, 10 Sep 2013 13:21:41 +0100 X-Google-Sender-Auth: cRDfBE92h7AITF8z3WWLKKVdHAg Message-ID: To: caml-list Content-Type: text/plain; charset=ISO-8859-1 Subject: [Caml-list] Using OCaml from C Dear List, I want to use my ocaml code from a C program. I have read the section "Interfacing C with OCaml" in the manual: http://caml.inria.fr/pub/docs/manual-ocaml/manual033.html The example in section 19.8 works fine. The compilation commands are (in a Makefile; OCAMLPATH points to /home/tr61/.opam/4.00.1): 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 (I had to add -lm and -ldl to get the example to work). If my code makes use of the OCaml Unix library, I do: cc -o prog main.c mod.a -lcurses -lm -ldl -L$(OCAMLPATH)/lib/ocaml -lunix And if my code makes use of threads, I do: cc -o prog main.c mod.a -lcurses -lm -ldl -L$(OCAMLPATH)/lib/ocaml -lunix -lthreads -lpthread 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: 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 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: -- /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 fixed by adding -pthread after cc command -- /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' 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? -- /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:(.text+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 fixed by adding -Xlinker --allow-multiple-definition to cc options -- My real question is: what command *should* I be using to compile my example? Or is the above more-or-less expected? Thanks Tom