caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Using OCaml from C
@ 2013-09-10 12:21 Tom Ridge
  2013-09-10 12:41 ` David Allsopp
  2013-09-10 13:09 ` Stéphane Glondu
  0 siblings, 2 replies; 10+ messages in thread
From: Tom Ridge @ 2013-09-10 12:21 UTC (permalink / raw)
  To: caml-list

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2013-09-12  9:51 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-10 12:21 [Caml-list] Using OCaml from C Tom Ridge
2013-09-10 12:41 ` David Allsopp
2013-09-10 13:21   ` Gerd Stolpmann
2013-09-10 13:09 ` Stéphane Glondu
2013-09-10 13:30   ` David Allsopp
2013-09-10 13:59     ` Tom Ridge
2013-09-10 14:21     ` Stéphane Glondu
     [not found]       ` <CABooLwPmKWc0KAOZ2weuwAfBbXUUKhBa7rxJ+=LASVL-0KeBmA@mail.gmail.com>
2013-09-10 17:21         ` Fwd: " Tom Ridge
2013-09-10 17:27           ` Adrien Nader
2013-09-12  9:51             ` Stéphane Glondu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).