From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id AAA06807; Fri, 29 Nov 2002 00:40:03 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id AAA06675 for ; Fri, 29 Nov 2002 00:40:02 +0100 (MET) X-SPAM-Warning: Sending machine is listed in blackholes.five-ten-sg.com Received: from athlon.baretta.com (host225-68.pool80116.interbusiness.it [80.116.68.225]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id gASNe1X16637 for ; Fri, 29 Nov 2002 00:40:01 +0100 (MET) Received: from baretta.com (localhost.localdomain [127.0.0.1]) by athlon.baretta.com (Postfix) with ESMTP id 89E66273E5; Fri, 29 Nov 2002 00:40:53 +0100 (CET) Message-ID: <3DE6A985.4000602@baretta.com> Date: Fri, 29 Nov 2002 00:40:53 +0100 From: Alessandro Baretta Organization: Baretta srl -- www.baretta.com User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.1) Gecko/20020826 X-Accept-Language: it, en MIME-Version: 1.0 To: Dmitry Boulytchev , Ocaml Subject: Re: [Caml-list] Reference to undefined global using Dynlink module. References: <115614851699.20021128200805@mail.tepkom.ru> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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 = ... 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 = 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