Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Linking mutually dependent modules for fun
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-10-16 (16:41)
From: Damien Guichard <alphablock@o...>
Subject: Re: [Caml-list] Linking mutually dependent modules for fun
Hi Dawid,

Have you ever considered recursive modules ?

- damien

En réponse au message
de : Dawid Toton
du : 2009-10-16 09:51:57
À :
CC : 
Sujet : [Caml-list] Linking mutually dependent modules for fun

I often get into trouble of cyclic dependencies when writing in OCaml.
Perhaps, I'm still not enough mentally shifted from poor languages (for
which circular design is quite natural).

Of course OCaml has many tools to deal with this: functors (which, once
introduced, spread like a disease), type variables and generalization
(which sometimes deadly collide with the functorial way) and ugly global
ref cells (which smell).

Why not try some Pascal style then? ;)
The following is what I got when studying OCaml's linking issues:

A.mli: val flip : unit - > unit let flip () = print_string "1"; B.flop ()  let _ = flip ()
B.mli: val flop : unit - > unit let flop () = print_string "0"; A.flip () let _ = print_endline "C"

ocamlopt -c -S
ocamlopt -c -S

The resulting .o depends on existing cmx files, so we can reach fixpoint
  by doing it once more:
ocamlopt -c -S

The new compiled A happens to work with uinitialized B.
Then use some helper module:
ocamlopt -c -S
ocamlopt -dstartup -ccopt --verbose C.cmx

Modify a.startup.s to call camlA_entry instead of camlC__entry.
as -o startup.o a.out.startup.s

Verbose gcc invocation gave us proper linking command.
...std_exit.o C.o /...../stdlib.a ...
with just
...std_exit.o A.o B.o /...../stdlib.a ...

And go!

OK, I must admit that I don't know how to get startup code that would
work in general. This one had wrong memory maps and no initialization of
B... anyway it works. :)


Caml-list mailing list. Subscription management:
Beginner's list:
Bug reports: