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] 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:
open Dynlink

let foo = <whatever>

let _ = loadfile "b.cmo"

... ...

Thi is your problem. When you try to access, 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 file which is 
initialized last--that is, linked last--where you call the 
Dynlink library to load Here's how to do it:
let foo = <whatever>
... ....
open Dynlink

let _ = loadfile "b.cmo"

all : dynlinked_prog b.cmo

b.cmo :
	ocamlc -c

	ocamlc -o $@
# 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 is 
defined); then, main is initialized (and Dynlink.loadfile is 
called). Thus, when loadfile is called, no dynamic linking 
error occurs.


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