A uses
clause consists of the reserved word uses, followed by one or more
comma-delimited unit names, followed by a semicolon. Examples:
uses
Forms, Main;
uses
Windows, Messages, SysUtils, Strings, Classes, Unit2, MyUnit;
uses
SysUtils, Types, Classes, QGraphics, QControls, QForms, QDialogs;
In the uses
clause of a program or library, any unit name may be followed by the reserved
word in and the name of a source file, with or without a directory path,
in single quotation marks; directory paths can be absolute or relative.
Examples:
uses
Windows, Messages, SysUtils, Strings in 'C:\Classes\Strings.pas',
Classes;
uses
QForms,
Main,
Extra in
'../extra/extra.pas';
Include in
... after a unit name when you need to specify the unit’s source file. Since
the IDE expects unit names to match the names of the source files in which they
reside, there is usually no reason to do this. Using in is necessary
only when the location of the source file is unclear, for example when
·You have used a source file that is in a different
directory from the project file, and that directory is not in the compiler’s
search path or the general Library search path.
·Different directories in the compiler’s search
path have identically named units.
·You are compiling a console application from the
command line, and you have named a unit with an identifier that doesn’t match
the name of its source file.
The
compiler also relies on the in ... construction to determine which units
are part of a project. Only units that appear in a project (.dpr) file’s uses
clause followed by in and a file name are considered to be part of the
project; other units in the uses clause are used by the project without
belonging to it. This distinction has no effect on compilation, but it affects
IDE tools like the Project Manager and Project Browser.
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, the general Library search path, or the same directory as the unit that uses it. Moreover, unit names must match the names of their source files.
Program structure and syntax:
Overview
Unit structure and syntax: Overview
Unit references and
the uses clause
Multiple and indirect
unit references
Multiple and indirect
unit references
Unit references and
the uses clause
uses子句由保留字uses、一个或多个单元名(多个单元名之间用逗号“,”隔开)、分号“;”顺序组成。例如:
uses
Forms, Main;
uses
Windows, Messages, SysUtils, Strings, Classes, Unit2, MyUnit;
uses
SysUtils, Types, Classes, QGraphics, QControls, QForms, QDialogs;
对于程序(program)或库(library),可以在uses子句中的每个单元名之后跟随保留字in及相应的源文件名,甚至可以根据需要决定是文件名否含有目录路径,文件名需要用一对单引号括“'”起来;目录路径可以是绝对路径,也可以是相对路径。例如:
uses
Windows, Messages, SysUtils, Strings in 'C:\Classes\Strings.pas',
Classes;
uses
QForms,
Main,
Extra in
'../extra/extra.pas';
当需要指定单元的源文件时才有必要在单元名之后含有in...说明。由于IDE总是自动试图将uses子句中的单元名与当前单元所在目录路径中的同名单元(.pas甚至是.dcu)文件相匹配,所以这样做通常是没有有效依据的。一般情况下不必使用in...说明,除非编译器不清楚源文件的位置时,例如下列情况就有必要使用in...说明:
· 工程中使用的单元,其源文件所在的目录与工程文件所在的目录不同,也不在编译器的搜索路径或通用库(VCL或CLX等)的搜索路径中时;
· 编译器搜索路径中,不同的路径中存在两个或更多同名单元(.pas或者.dcu)文件时;
· 以命令行方式编译控制台应用程序,而使用的单元与其相应的源文件名称不匹配时。
编译器也依靠in...说明来确定单元是否作为工程(这里的工程是泛指工程中的所有资源,包括工程文件、单元文件等)的一部分。只有那些出现在工程(.dpr)文件的uses子句中,并且相应的单元名之后跟随有in...说明的单元,才被视为工程的一部分,而其他出现在uses子句中的单元尽管被工程使用,但它们不属于该工程。这一区别不会影响编译,但会影响IDE工具,如项目管理器(Project Manager)、项目浏览器(Project Browser)等。
在单元中的uses子句不能使用in...说明来告诉编译器源文件所在的位置。出现在uses子句中的所有单元必需位于编译器的搜索路径、通用包搜索路径或与使用该单元的单元在相同路径中(三者至少满足其中之一)。此外,单元名必需与其相应的源文件名相匹配。
编者注
使用Borland开发工具进行Object Pascal编程中,Project一词在不同的场合具有不同的含义。当与单元以示区别时,其含义是工程、工程文件或主程序文件,属于源代码模块的特指;当与库(library)、包(package)等以示区别时,有时是特指工程文件(保留字program的区别),有时是泛指工程、程序,即函盖了单元以及其他属于工程的资源,相当于总称;当与若干应用程序、库、包、组件等相提并并论时,也是对程序组或程序包的总称,这时比较恰当的含义应为“项目”。因此,在本参考中,根据不同情况对Project一词的翻译也有所区别。