参考:
- https://docwiki.embarcadero.com/RADStudio/Florence/en/Delphi_Language_Reference
- https://docwiki.embarcadero.com/RADStudio/Florence/en/Language_Overview
源文件
三类pascal源文件:
- Unit source files (which end with the .pas extension)
- Project files (which end with the .dpr extension)
- Package source files (which end with the .dpk extension):包源文件与项目文件相似,但用于构建称为包的特殊动态可链接库。
大多数程序都以program开头,为程序指定名称, uses 语句列出了与程序相关联的Unit
non-Pascal files to build applications. These files are maintained automatically by the IDE, and include
- VCL form files (which have a .dfm extension on Win32)
- Resource files (which end with .res)
- Project options files (which end with .dof )
- 每个项目使用资源(.res)文件来保存应用程序的图标以及其他资源(如字符串)。默认情况下,此文件与项目(.dpr)文件具有相同的名称。
- 项目选项(.dof)文件包含编译器和链接器设置、搜索路径信息、版本信息等。每个项目都有一个与项目(.dpr)文件名称相同的项目选项文件。通常,此文件中的选项由项目选项对话框设置。
- IDE 中的各种工具将数据存储在其他类型的文件中。桌面设置(.dsk)文件包含有关窗口安排和其他配置选项的信息;桌面设置可以是项目特定或全环境的。这些文件对编译没有直接影响。
每个表单文件代表一个表单,通常对应一个窗口或一个对话框。每个项目至少有一个表单,每个表单都有一个关联的单元(.pas)文件,默认情况下该文件与表单文件具有相同的名称。
编译器生成的文件:
- 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.
示例程序
单文件
示例程序: helloworld.pas
program Greeting;
{$APPTYPE CONSOLE}
var
MyMessage: string;
begin
MyMessage := 'Hello world!你好世界';
Writeln(MyMessage);
end.
- windows程序编译器:dcc32,dcc64
- Writeln is defined implicitly in the System unit, which the compiler automatically includes in every application.
- 解决输出中中文乱码问题:源码存为GBK/GB18030
调用单元文件的例子
helloworld.pas
program Greeting;
{$APPTYPE CONSOLE}
uses
Unit1;
begin
PrintMessage('Hello World!你好世界!');
end.
Unit1.pas (文件名必須和uses相同 Unit1.pas:)
unit Unit1;
interface
procedure PrintMessage(msg: string);
implementation
procedure PrintMessage(msg: string);
begin
Writeln(msg);
end;
end.
- PrintMessage is declared twice in Unit1.
- The first declaration, under the reserved word interface, makes PrintMessage available to other modules (such as greeting) that use Unit1.
- The second declaration, under the reserved word implementation, actually defines PrintMessage.
VCL示例
自动生成,使用了类和对象,修改项目文件名 Project1.dproj 为 helloworld.dproj,其他文件名会自动修改。
helloworld.dpr
program helloworld;
uses
Vcl.Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
- units: Forms, which is part of VCL;
- Unit1, which is associated with the application's main form (Form1);
- object named Application, which is an instance of the Vcl.Forms.TApplication class defined in the Forms unit. (Every project has an automatically generated Application object.)
- Vcl.Forms.TApplication method named CreateForm. The call to CreateForm creates Form1, an instance of the TForm1 class defined in Unit1.
Unit1.pas
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('helloworld!你好世界!');
end;
end.
- Unit1 creates a class named TForm1 (derived from Vcl.Forms.TForm) and an instance of this class Form1.
- The TForm1 class includes a button -- Button1, an instance of Vcl.StdCtrls.TButton -- and a procedure named Button1Click that is called when the user presses Button1.
- Button1Click call procedure TForm1.Button1Click
代码格式化
- 安装free pascal: https://www.freepascal.org/
- 安装vscode 插件: Pascal,Pascal Formatter,安装 Pascal Language Basics,Object Pascal Syntax Highlighter
- Pascal Formatter设置:文件,首选项,设置,扩展,Pascal Formatter
- 引擎:ptop
- 路径:
C:\\FPC\\3.2.2\\bin\\i386-win32\\ptop.exe - 配置文件路径:
C:\\FPC\\3.2.2\\bin\\i386-win32\\ptop.cfg
正文完