English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
C libs from Ocaml libs
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-11-22 (17:21)
From: Alain Frisch <alain@f...>
Subject: Re: [Caml-list] C libs from Ocaml libs
viktor tron wrote:
> sorry, let me be very explicit then:
> 0)
> I believed that the following
> creates the c object containing:
> (a) main ocaml implementation of foo (b) export API (c) C binding to API 
> (d) ocaml startup code
> $ ocamlopt -output-obj -o foo_caml.o foo.cmxa foo_stub.c foo_export.ml

You are correct.

> now I create a lib
> $ ar rs libfoo.a foo_caml.o
> and happy ever after. Nope.
> No matter how I link it with a main c test, I get undefined symbols for startup code 
> $ gcc -o foo_test foo_test.c -L. -lfoo -lasmrun

What happens if you pass foo_caml.o instead of -lfoo?

Can you send me (maybe off-list) a full set of files to reproduce the 

> In fact, the following variants don't work:
> 1)
> $ ocamlopt -output-obj -o foo_caml.o foo.cmxa foo_stub.c foo_export.ml
> $ ar -rs libfoo.a /sw/lib/ocaml/libasmrun.a foo_caml.o
> $ gcc -o foo_test foo_test.c -L. -lfoo

I don't think that ar supports merging libraries like that. What can be 
done is to use "ld -r" (partial linking) to produce a single object file
from libasmrun.a and foo_caml.o and then put this file in a library (or 
use it directly).

> 2)
> $ ocamlopt -output-obj -o foo_caml.o foo.cmxa foo_stub.c foo_export.ml
> $ cp /sw/lib/ocaml/libasmrun.a libfoo.a
> $ ar -rs libfoo.a foo_caml.o

Ok, that's a hack to merge one library, plus more object files into a 
library. I know it is mentioned in the OCaml manual. Personally, I would 
not recommend to do that (you'll be stuck if you need to include e.g. 

> In fact ocamlopt does pretty enigmatic things in the background

Use the "-verbose" option to see what's going on.

> and ar 
> is an entire mystery with this
> copy thing.

Use "ar t" to see what's in a library.

-- Alain