Quick start manual
Programs and units
3-7
Unit references and the uses clause
In the uses clause of a unit, you cannot use in to tell the compiler where to find a
source file. Every unit must be in the compiler’s search path. Moreover, unit names
must match the names of their source files.
Multiple and indirect unit references
The order in which units appear in the uses clause determines the order of their
initialization (see “The initialization section” on page 3-4) and affects the way
identifiers are located by the compiler. If two units declare a variable, constant, type,
procedure, or function with the same name, the compiler uses the one from the unit
listed last in the uses clause. (To access the identifier from the other unit, you would
have to add a qualifier: UnitName.Identifier.)
A uses clause need include only units used directly by the program or unit in which
the clause appears. That is, if unit A references constants, types, variables,
procedures, or functions that are declared in unit B, then A must use B explicitly. If B
in turn references identifiers from unit C, then A is indirectly dependent on C; in this
case, C needn’t be included in a uses clause in A, but the compiler must still be able to
find both B and C in order to process A.
The following example illustrates indirect dependency.
program Prog;
uses Unit2;
const a = b;
ƒ
unit Unit2;
interface
uses Unit1;
const b = c;
ƒ
unit Unit1;
interface
const c = 1;
ƒ
In this example, Prog depends directly on Unit2, which depends directly on Unit1.
Hence Prog is indirectly dependent on Unit1. Because Unit1 does not appear in Prog’s
uses clause, identifiers declared in Unit1 are not available to Prog.
To compile a client module, the compiler needs to locate all units that the client
depends on, directly or indirectly. Unless the source code for these units has
changed, however, the compiler needs only their .dcu (Windows) or .dcu/.dpu
(Linux) files, not their source (.pas) files. If the link partner is a package.
When a change is made in the interface section of a unit, other units that depend on
the change must be recompiled. But when changes are made only in the
implementation or other sections of a unit, dependent units don’t have to be
recompiled. The compiler tracks these dependencies automatically and recompiles
units only when necessary.