Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Reference to undefined global using Dynlink module.
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Alessandro Baretta <alex@b...>
Subject: Re: [Caml-list] Reference to undefined global using Dynlink module.


Dmitri Boulytchev wrote:
>         Hello everybody,
> 
>         I didn't get as answer browsing the archive so I decided to ask.
>         
>         How to avoid "Reference to undefined global ***" error during
>         loading *.cmo via Dynlink  module?

I had the same problem so I am competent on the subject. I 
bet your code looks like the following:

a.ml
***********
open Dynlink

let foo = <whatever>

...
let _ = loadfile "b.cmo"

b.ml
***********


... A.foo ...

      ^
      |
      |
Thi is your problem. When you try to access A.foo, module A 
has not completed initialization, so it's values appear to 
be yet undefined to all other modules.

What you have to do is create a main.ml file which is 
initialized last--that is, linked last--where you call the 
Dynlink library to load b.ml. Here's how to do it:

a.ml
***************
let foo = <whatever>


b.ml
***************
... A.foo ....


main.ml
***************
open Dynlink

let _ = loadfile "b.cmo"


Makefile
*******************
all : dynlinked_prog b.cmo

b.cmo : b.ml
	ocamlc -c b.ml

dynlinked_prog: a.ml main.ml
	ocamlc -o $@ a.ml main.ml
# notice the order in which the files are compiled

*******************

When you run make you get an executable and a CMO. When you 
run the executable, all the modules that are statically 
linked into it are initialized in the order they were 
specified in. So first A is initialized (and A.foo is 
defined); then, main is initialized (and Dynlink.loadfile is 
called). Thus, when loadfile is called, no dynamic linking 
error occurs.

Alex


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners