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 (09:05)
From: Richard Cole <rcole@i...>
Subject: [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. Let me give an example:

rm -f ml_funky.c *funky*.cm* *funky*.a *funky*.so

echo '
  Printf.fprintf stderr "Test.Init\n"; flush stderr ;;
  open Funky ;;
  funky 4 ;;
' >

echo '
  #include <caml/mlvalues.h>
  #include <caml/memory.h>
  #include <stdio.h>

  value ml_funky(value v)
    int i = Int_val(v) + 1;
    fprintf(stderr, "ML_FUNKY!\n"); fflush(stderr);
' > ml_funky.c

echo '
  external funky : int -> int = "ml_funky" ;;
  Printf.fprintf stderr "Funky.Init.\n"; flush stderr ;;
' >

gcc -Wall -c ml_funky.c -I/usr/local/godi/lib/ocaml/std-lib/
ocamlopt -c
ocamlopt -c
ocamlmklib -o funky ml_funky.o funky.cmx
ocamlopt -o test_funky.exe funky.cmxa test_funky.cmx -I .

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:

echo '
  external ml_funky: int -> int = "ml_funky" ;;
  let funky x = ml_funky x ;;
  Printf.fprintf stderr "Init.\n"; flush stderr ;;
' >

ocamlopt -c
ocamlmklib -o funky ml_funky.o funky.cmx
ocamlopt -o test_funky.exe funky.cmxa -I .

Then the initialization code, namely the printf statement in 
is called.

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?

I should mention that I'm using Godi with godi-ocaml version 3.07pl2 and 
fairly vanilla Redhat 9. Is this problem replicatable on other platforms?

Why do I want to have intialization code always called in my modules. 
Mostly because I want the following initialization code to work without 
having to add a layer of indirection to my wrapper modules.

exception NotFound ;;
let _ = Callback.register "Funky.NotFound" NotFound ;;

best regards,


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