Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Alain Frisch <alain@frisch.fr>
To: viktor tron <viktor.tron.ml@gmail.com>
Cc: Caml Mailing List <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] C libs from Ocaml libs
Date: Mon, 26 Nov 2007 18:20:00 +0100	[thread overview]
Message-ID: <474B0040.4080706@frisch.fr> (raw)
In-Reply-To: <8cc3d8520711211035w6c9fa3a6je4622195e5a752af@mail.gmail.com>

viktor tron wrote:
> If anyone can make me understand the above, I'd be forever grateful.

Here is the more direct way to achive what you want:

===========================================================
ocamlopt -output-obj -o foo_caml.o foo.cmxa foo_export.ml
ocamlopt -c foo_stub.c
gcc -o foo_test foo_test.c foo_stub.o foo_caml.o \
    $(OCAMLLIB)/libasmrun.a -lm -ldl
===========================================================


Note that "ocamlopt -output-obj" uses the so-called partial linking 
feature (ld -r) to produce a single object file that contains:
- the startup code
- all the native OCaml code (including the code of the libraries, like 
  foo.a and stdlib.a)

But *not* the extra C objects and libraries (-ccopt, *.c/*.o/*.a 
arguments). (Thanks to Rabih for this off-line reminder.) I don't know 
whether this qualifies as a bug or as a feature.

Here is a variant where we pack foo_caml.o and foo_stub.o together in a 
single object using the partial linker manually:

===========================================================
ocamlopt -output-obj -o foo_caml.o foo.cmxa foo_export.ml
ocamlopt -c foo_stub.c
ld -r -o libfoo.o foo_stub.o foo_caml.o	
gcc -c foo_test.c
gcc -o foo_test foo_test.c libfoo.o $(OCAMLLIB)/libasmrun.a -lm -ldl
===========================================================

We can do more and also put the libasmrun.a library in the same object:

===========================================================
ocamlopt -output-obj -o foo_caml.o foo.cmxa foo_export.ml
ocamlopt -c foo_stub.c
ld -r -o libfoo.o foo_stub.o foo_caml.o	$(OCAMLLIB)/libasmrun.a
gcc -c foo_test.c
gcc -o foo_test foo_test.c libfoo.o -ldl -lm
===========================================================

One could try to put foo_stub.o and foo_caml.o together in a library:

===========================================================
ocamlopt -output-obj -o foo_caml.o foo.cmxa foo_export.ml
ocamlopt -c foo_stub.c
ar r libfoo.a foo_stub.o foo_caml.o	
gcc -c foo_test.c
gcc -o foo_test foo_test.c libfoo.a $(OCAMLLIB)/libasmrun.a -lm -ldl
===========================================================

This does not work because the two libraries libfoo.a and libasmrun.a 
depend on symbols defined in the other library. This can be addressed 
with the linker options --start-group/--end-group:

===========================================================
ocamlopt -output-obj -o foo_caml.o foo.cmxa foo_export.ml
ocamlopt -c foo_stub.c
ar r libfoo.a foo_stub.o foo_caml.o	
gcc -c foo_test.c
gcc -o foo_test foo_test.c \
  -Wl,--start-group libfoo.a $(OCAMLLIB)/libasmrun.a -Wl,--end-group \
  -lm -ldl
===========================================================


In the current development version of OCaml, it is also possible to go 
through a shared library:

===========================================================
ocamlopt -output-obj -o foo_caml.so foo.cmxa foo_export.ml foo_stub.c
gcc -c foo_test.c
gcc -o foo_test foo_test.c foo_caml.so -Wl,-R.
===========================================================

When -output-obj is used to produce a shared library, it will put extra 
C objects and options (here, foo_stub.o, libasmrun.a -lm, -ldl) in the 
result.

Feel free to ask more questions (and to write a tutorial on this issues)!


Alain


  parent reply	other threads:[~2007-11-26 17:20 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-21  7:43 viktor tron
2007-11-21  9:45 ` [Caml-list] " Alain Frisch
2007-11-21 11:21   ` Matthieu Dubuget
2007-11-21 12:05     ` Alain Frisch
2007-11-21 19:48       ` Jon Harrop
2007-11-21 17:06   ` viktor tron
2007-11-21 17:33     ` Alain Frisch
2007-11-21 18:35       ` viktor tron
2007-11-22 17:21         ` Alain Frisch
2007-11-26 17:20         ` Alain Frisch [this message]
2007-11-22 16:41 RABIH.ELCHAAR

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=474B0040.4080706@frisch.fr \
    --to=alain@frisch.fr \
    --cc=caml-list@yquem.inria.fr \
    --cc=viktor.tron.ml@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox