Browse thread
C libs from Ocaml libs
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Alain Frisch <alain@f...> |
| Subject: | Re: [Caml-list] C libs from Ocaml libs |
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