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
[Caml-list] Indeterminate Initialization: Is it a Bug?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-04-01 (13:39)
From: Xavier Leroy <xavier.leroy@i...>
Subject: Re: [Caml-list] Indeterminate Initialization: Is it a Bug?
> There seems to be a difference in initialization behaviour using the 
> native compiler depending on whether a module contains non "external" 
> calls. [...]

> The output doesn't contain "Funky.Init". The problem is that the 
> initialization code in the funky module didn't get called. If I wrap the 
> functions as in:
> #!/bin/bash
> echo '
>   external ml_funky: int -> int = "ml_funky" ;;
>   let funky x = ml_funky x ;;
>   Printf.fprintf stderr "Init.\n"; flush stderr ;;
> ' >

A simpler solution is to define "funky" as an external in the .ml file
but declare it as a regular value in the .mli file:

.ml:  external funky: int -> int = "ml_funky"
.mli: val funky: int -> int

> So I have the following question: Is there some way to guarrentee that 
> modules are initialized exactly once? Does the development team see the 
> current situation, whereby intialization depends on wheher there is a 
> non external method in the module that is called, as a problem? Can it 
> be fixed?

The behavior you observe is a consequence of the link-time elimination
of unreferenced .cma or .cmxa members.  An "external" declaration in a
module interface is like a type declaration: using it elsewhere
doesn't create a reference to the implementation of the defining module.

You can turn off this link-time elimination using the -linkall flag,
either at library creation time (ocamlmklib), or at link-time (ocamlopt).
Then, all modules will be initialized exactly once and in the order
given, like you expect.

- Xavier Leroy

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: