Delphi XE5教程2:程序组织


内容源自Delphi XE5 UPDATE 2官方帮助《Delphi Reference》,本人水平有限,欢迎各位高人修正相关错误!

也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者可QQ:[email protected]


1 Program Organization

1         程序组织

Delphi programs are usually divided into source-code modules called units. Most programs begin with a program heading, which specifies a name for the program. The program heading is followed by an optional uses clause, then a block of declarations and statements. The uses clause lists units that are linked into the program; these units, which can be shared by different programs, often have uses clauses of their own.

应用程序通常被分成多个源代码模块,我们称它们为单元(unit)。多数程序以程序头(program)开始,它为程序指定一个名称。在程序头之后是一个可选的uses 子句,然后是一个由声明和命令语句组成的块(block)。uses 子句列出了那些链接到程序的单元,这些单元可以被不同的程序共享,并且通常有自己的uses 子句。

The uses clause provides the compiler with information about dependencies among modules. Because this information is stored in the modules themselves, most Delphi language programs do not require makefiles, header files, or preprocessor "include" directives.

uses 子句给编译器提供各模块间的依赖信息,因为这些信息是存于模块自身的,所以,Object Pascal程序不需要makefile 文件、头文件以及include 预处理指令。


1.1   Delphi Source Files

1.1   Delphi源文件

The compiler expects to find Delphi source code in files of three kinds:

编译器期望在以下三种文件中取得 Pascal 源代码:

  • Unit source files (which end with the .pas extension)

单元源文件 (文件扩展名为 .pas

  • Project files (which end with the .dpr extension)

工程文件 (文件扩展名为 .dpr

  • Package source files (which end with the .dpk extension)

包源文件 (文件扩展名为 .dpk

Unit source files typically contain most of the code in an application. Each application has a single project file and several unit files; the project file, which corresponds to the program file in traditional Pascal, organizes the unit files into an application. Embarcadero development tools automatically maintain a project file for each application.

单元源文件包含程序代码的主要部分,每个程序包含一个工程文件和多个单元文件。工程文件相当于传统Pascal 语言的‘主’程序文件,它把各单元文件组织成一个程序。Embarcadero 开发工具自动为每一个程序维护一个工程文件。

If you are compiling a program from the command line, you can put all your source code into unit (.pas) files. If you use the IDE to build your application, it will produce a project (.dpr) file.

如果从命令行编译一个程序,你可以把所有源代码放在单元文件(.pas)中,但如果用IDE 创建程序,则必须有一个工程文件(.dpr)。

Package source files are similar to project files, but they are used to construct special dynamically linkable libraries called packages.



1.2 Other Files Used to Build Applications


In addition to source-code modules, Embarcadero products use several non-Pascal files to build applications. These files are maintained automatically by the IDE, and include

除了源代码文件,Embarcadero工具还需要几种非Pascal 文件来创建程序。这些文件是由IDE自动维护的,包括以下文件:

  • VCL form files (which have a .dfm extension on Win32)

窗体文件(Win32平台上扩展名为 .dfm)

  • Resource files (which end with .res)

资源文件,(扩展名为 .res )(已经编译的资源文件)

  • Project options files (which end with .dof )

工程选项文件,(扩展名为 .dof

A VCL form file contains the description of the properties of the form and the components it owns. Each form file represents a single form, which usually corresponds to a window or dialog box in an application. The IDE allows you to view and edit form files as text, and to save form files as either text (a format very suitable for version control) or binary. Although the default behavior is to save form files as text, they are usually not edited manually; it is more common to use Embarcadero's visual design tools for this purpose. Each project has at least one form, and each form has an associated unit (.pas) file that, by default, has the same name as the form file.

一个VCL窗体文件包含窗体属性和它包含组件的描述。每个窗体文件表示一个窗体,通常对应于程序中的一个窗口或对话框。IDE 允许以文本方式察看和编辑窗体文件,并且能以文本或二进制格式保存它。虽然默认是以文本方式保存窗体,但通常不要手动编辑它,更常用的方式是使用Embarcadero 提供的可视化设计工具。每个工程至少有一个窗体,每个窗体有一个关联的单元文件(.pas),默认情况下,单元的文件名和窗体文件名相同。

In addition to VCL form files, each project uses a resource (.res) file to hold the application's icon and other resources such as strings. By default, this file has the same name as the project (.dpr) file.


A project options (.dof) file contains compiler and linker settings, search path information, version information, and so forth. Each project has an associated project options file with the same name as the project (.dpr) file. Usually, the options in this file are set from Project Options dialog.

工程选项文件(.dof)包含编译器和链接器设置、搜索路径以及版本信息等等。每个工程对应一个选项文件,它和工程文件同名。通常情况下,文件中的选项是通过Project Options 对话框来完成的。

Various tools in the IDE store data in files of other types. Desktop settings (.dsk) files contain information about the arrangement of windows and other configuration options; desktop settings can be project-specific or environment-wide. These files have no direct effect on compilation.

IDE 中的许多工具保存其它类型的文件。桌面设置文件(.dsk)包含窗口的排列信息及其它设置项目。桌面设置或者特定于一个工程(和某个工程相关),或者作用于整个环境(environment-wide)(不是特定于某个工程,对所有工程都有效)。这些文件对编译没有影响。


1.3 Compiler-Generated Files


The first time you build an application or a package, the compiler produces a compiled unit file (.dcu on Win32) for each new unit used in your project; all the .dcu files in your project are then linked to create a single executable or shared package. The first time you build a package, the compiler produces a file for each new unit contained in the package, and then creates both a .dcp and a package file. If you use the GD compiler switch, the linker generates a map file and a .drc file; the .drc file, which contains string resources, can be compiled into a resource file.

在第一次生成一个程序或一个包时,编译器为工程中所使用的每个新单元创建一个编译(过的)单元文件(在Windows上为.dcu)。工程中所有的.dcu 文件被链接到一个单独的可执行文件或共享包中;当第一次生成一个包时,编译器为包所包含的每个新单元创建一个.dcu 文件,然后创建.dcp 文件和包文件。若使用–GD 开关,链接器生成一个map 文件和.drc 文件,.drc文件包含字符串资源,能被编译进资源文件中。

When you build a project, individual units are not recompiled unless their source (.pas) files have changed since the last compilation, their .dcu/.dpu files cannot be found, you explicitly tell the compiler to reprocess them, or the interface of the unit depends on another unit which has been changed. In fact, it is not necessary for a unit's source file to be present at all, as long as the compiler can find the compiled unit file and that unit has no dependencies on other units that have changed.

当重新生成一个工程时,除非自上次编译后单元文件(.pas)发生了改变、或者没发现和.dcu/.dpu 文件、或者明确告诉编译器重新编译它,或者单元的接口依赖的另一个单元已经改变。实际上,没有必要为一个单元源文件去编译所有的单元文件。只要编译器能找到编译(过的)单元文件,而该单元和已经改变的其它单元没有依赖关系。