A program
is constructed from source-code modules called units. Each unit is
stored in its own file and compiled separately; compiled units are linked to
create an application. Units allow you to
·divide large programs into modules that can be
edited separately.
·create libraries that you can share among
programs.
·distribute libraries to other developers without
making the source code available.
In traditional Pascal programming, all source code, including the main program, is stored in .pas files. Borland tools use a project (.dpr) file to store the main program, while most other source code resides in unit (.pas) files. Each application or project consists of a single project file and one or more unit files. (Strictly speaking, you needn’t explicitly use any units in a project, but all programs automatically use the System unit.) To build a project, the compiler needs either a source file or a compiled unit file for each unit.
Program structure and syntax:
Overview
Unit structure and syntax: Overview
Unit references and
the uses clause
Multiple and indirect
unit references
译文
由源代码模块的程序叫做单元。每个单元保存在各自相应的文件中并且分别编译;编译过的单元被连接用于创建应用程序。单元允许开发者
· 将大的程序组分成可以分别编辑的若干模块。
· 在程序组中创建共享库。
· 向其他的开发者分发库(编译过的单元)而保留源代码。
在传统的Pascal编程中,所有的源代码,包括主程序,都保存在.pas文件中。Borland开发工具用工程(.dpr)文件存储主程序,而将大多数其他的源代码驻留在单元(.pas)文件中。每个应用程序或工程都是由一个工程文件和一个或更多的单元文件组成。(严格地说,尽管有时在工程中不需要明确通过uses子句使用任何单元,但所有的程序都会自动使用System单元。)要建立一个工程,编译器需要每个单元的源代码(.pas)文件或编译过的单元(.dcu)文件。
编者注
编译器会为工程(.dpr)文件和其他所有单元(.pas)文件自动使用标准单元System,可见System单元有其特殊意义,否则完全可以象Windows、Messages、SysUtils等单元一样自动显式地添加到uses子句的单元列表中即可。如果可以将System单元显式地添加到uses子句的单元列表中,相信Borland一定会这样做。而事实上,根据Object Pascal语言中对uses子句和作用域的规定,无论是将System单元添加到接口节的uses子句中还是添加到实现节的uses子句中,也无论是作为uses子句中单元列表的第一个还是最后一个,编译器都无法根据作用域规则确定System单元的作用域在最外围(即作用域最广,因此具有对同名例程、变量等语法元素最高的优先调用权)。因此,System单元成为唯一被所有程序和单元隐式自动使用的特殊单元。详细论述见作用域。