Re: compiling multifile program

From: Pierre Weis (
Date: Mon Apr 14 1997 - 17:14:57 MET DST

From: Pierre Weis <>
Message-Id: <>
Subject: Re: compiling multifile program
In-Reply-To: <> from Lyn A Headley at "Apr 11, 97 08:04:44 pm"
To: (Lyn A Headley)
Date: Mon, 14 Apr 1997 17:14:57 +0200 (MET DST)

[Résumé en Français]
[Le problème vient de l'ordre dans lequel sont donnés vos fichiers lors
de l'édition des liens: cet ordre est important en Caml, car les
définitions donnent lieu à des calculs arbitraires et doivent être
effectuées dans l'ordre de présentation donné par le programmeur.]

> ok, so I tried to compile the simplest multifile program I could
> think of, and I still get the "Undefined Global" error. The files
> are[i],[i], and
> The error is:
> Error while linking response2.cmo: Reference to undefined global `Qstring2'
> make: *** [all] Error 2
> Lyn Headley
> In case it should matter, Here's the Makefile:
> OCC=ocamlc
> OCDEP=ocamldep
> INCLUDES= #all relevant -I options here
> OCFLAGS=$(INCLUDES) -custom str.cma unix.cma -cclib -lunix -cclib -lstr
> # quiz should be compiled to bytecode, and is composed of three
> # units: mod1, mod2 and mod3.
> # Common rules
> .SUFFIXES: .ml .mli .cmo .cmi
> .mli.cmi:
> $(OCC) -c $<
> .ml.cmo:
> $(OCC) -c $<
> QUIZ_OBJS= entry2.cmo qstring2.cmo
> all: $(QUIZ_OBJS)

Your make file generates the following linking command:

ocamlc -custom str.cma unix.cma -cclib -lunix -cclib -lstr entry2.cmo qstring2.cmo

It is then clear that when linking response2.cmo (which is compiled on
the fly by your command) neihter entry2.cmo nor qstring2.cmo are

If you're puzzled by this explanation, remember that the order of
linking is relevant in Caml (and should be, since definitions may lead
to arbitrary computation that must be executed in order of
presentation). You should then start linking by entry2 or qstring2
that are self contained; afterwards you can link response2 that
depends on these two files.

I suggest to modify your make file. A quick and dirty hack is to
change the order of macros in the link command, from


         $(OCC) $(QUIZ_OBJS) $(OCFLAGS)

A better way is to add response2.cmo in the list of compiled files
used to built your program, being careful on the linking order:

QUIZ_OBJS= entry2.cmo qstring2.cmo response2.cmo

define OCFLAGS accordingly :

OCFLAGS=$(INCLUDES) -custom str.cma unix.cma -cclib -lunix -cclib -lstr

(by the way LINKFLAGS is more could be more appropriate)

Pierre Weis

INRIA, Projet Cristal,,

This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:10 MET