Each
package is declared in a separate source file, which should be saved with the
.dpk extension to avoid confusion with other files containing Object Pascal
code. A package source file does not contain type, data, procedure, or function
declarations. Instead, it contains
· A name for the package.
· A list of other packages required by the new package. These are packages to which the new package is linked.
· A list of unit files contained by, or bound into, the package
when it is compiled. The package is essentially a wrapper for these source-code
units, which provide the functionality of the compiled package.
A package
declaration has the form
package packageName;
requiresClause;
containsClause;
end.
where packageName is any valid
identifier. The requiresClause and containsClause are both
optional. For example, the following code declares the DATAX package.
package
DATAX;
requires
baseclx,
visualclx;
contains Db, DBLocal, DBXpress,
... ;
end.
The requires
clause lists other, external packages used by the package being declared. It
consists of the directive requires, followed by a comma-delimited list of
package names, followed by a semicolon. If a package does not reference other
packages, it does not need a requires clause.
The contains
clause identifies the unit files to be compiled and bound into the package. It
consists of the directive contains, followed by a comma-delimited list
of unit names, followed by a semicolon. 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. For example,
contains
MyUnit in 'C:\MyProject\MyUnit.pas'; // Windows
contains
MyUnit in '\home\developer\MyProject\MyUnit.pas'; // Linux
Note: Thread-local variables (declared with threadvar) in a packaged unit cannot be accessed from clients that use the package.
每个包在单独的源文件中声明,它们应保存为.dpk文件以避免与其他包含Object Pascal代码的文件混淆。包的源代码不能包含任何类型、数据或函数声明。包能包含:
· 包的名称。
· 在requires子句中列出新包需要利用的包,这些包将被连接到新的包。
· 当包被编译时,列于contains子句中的单元将作为被包含的单元联编到包中。包的实质就是这些源代码单元的特殊封装器,这些单元提供编译过的包的功能。
包的声明具有如下形式:
package packageName;
requiresClause;
containsClause;
end.
这里的packageName是一个有效的标识符。requiresClause和containsClause都是可选的。例如,下面的代码声明了DATAX包:
package
DATAX;
requires
baseclx,
visualclx;
contains Db, DBLocal, DBXpress,
... ;
end.
requires子句列出了其他外部包,这些包用于当前被声明的包。requires子句由指示字requires、随后的逗号隔开的包名称列表以及末尾分号组成。如果一个包不需要引用其他任何包,那么它不需要requires子句。
contains子句标识了包含于包并将联编到包中的所有单元。contains子句由指示字contains、随后的逗号隔开的单元文件名以及末尾分号组成。其中,任何单元名都可以跟随保留字in及一个源文件名,可以有或没有路径名,并且,文件名以单引号括起来,路径名(如果有)可以是绝对路径,也可以是相对路径。例如,
contains
MyUnit in 'C:\MyProject\MyUnit.pas'; //在Windows中
contains
MyUnit in '\home\developer\MyProject\MyUnit.pas'; //在Linux中
注意: 包单元中的线程局部变量(以保留字threadvar声明的变量)不能被使用了该包的客户(应用程序或DLLs)访问。