Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
FREEDOM251

Freedom251 › Blog › Delphi

免费 Pascal 语言参考:Free Pascal 完整指南(安装、语法与文档导航)

By Freedom251 Team · · 2 min read

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

直接结论:Free Pascal Compiler(FPC)是免费、开源的 Pascal/Object Pascal 编译器。官方 Language Reference Guide 负责解释语法和语义;安装、编译选项、运行库 API 及 Lazarus GUI 开发则应分别查阅其他手册。官方普通下载页目前标示的最新正式安装包是 FPC 3.2.2(2021 年 5 月 20 日发布),而 daily 文档已经提供 3.2.4 内容(文档日期为 2026 年 7 月),两者不要混为一谈。

Free Pascal、Pascal、Object Pascal 和 Lazarus 有什么区别

名称 它是什么
Pascal 一种编程语言及其语言家族。
Object Pascal 加入类、对象、异常、接口等能力的 Pascal 方言。
Free Pascal Compiler 编译 Pascal/Object Pascal 源代码的编译器,提供 RTL 和 FCL。
Lazarus 基于 FPC 的免费跨平台 IDE、调试器、表单设计器和 LCL GUI 框架。
Delphi Embarcadero 的商业 Object Pascal 工具链和生态,使用 VCL/FireMonkey 等专有库。

FPC 对 Turbo Pascal 7.0 和多数 Delphi 语言特性具有较强兼容性,但这不等于整个 Delphi 应用可以直接迁移。语言模式、单元、编译器指令、VCL/LCL、窗体资源、第三方组件和操作系统 API 都可能造成差异。

As an Amazon Associate I earn from qualifying purchases.

官方介绍见 Free Pascal 说明 和 User’s Guide 兼容性章节。

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

先打开哪一本官方手册

手册 适合解决的问题 链接
Language Reference Guide 关键字、类型、表达式、语句、过程、类、异常、泛型、单元、编译器模式。 当前参考手册;PDF
User’s Guide 安装、命令行、配置文件、搜索路径、输出、交叉编译和错误信息。 当前用户指南
Programmer’s Guide $MODE、条件编译、调用约定、优化、调试和链接指令。 当前程序员指南
RTL Reference SysUtils、Classes、Math、文件、字符串、日期、异常和线程 API。 官方文档总目录
FCL Reference 集合、XML、JSON、网络、数据库、压缩和更高层组件。 官方文档总目录

初学者可先看 语言概览,遇到规则再回到 Reference Guide;编译器选项查 User’s Guide,API 查 RTL/FCL,窗体和控件查 Lazarus 文档。

版本、下载与安装选择

官方 下载页 当前显示 FPC 3.2.2 为最新正式发行版。官方 FAQ 也以 3.2.2 作为稳定版参考(FAQ)。另一方面,daily 页面已有 3.2.4 的 Reference、User’s Guide 和 Programmer’s Guide:Reference、User’s Guide、Programmer’s Guide。这表示文档线已更新,并不自动证明 3.2.4 安装包已经作为正式版发布。

  • 学习、作业和生产构建:安装正式版。
  • 需要修复分支中的特定修复:考虑 3.2.x snapshot,但保留正式版作为回退。
  • 测试未来功能或参与开发:使用 3.3.x trunk snapshot;它可能含新问题,不适合作为新手或生产环境首套工具链。
  • 只学语言或写命令行程序:单独安装 FPC 和文本编辑器即可。
  • 制作桌面 GUI:安装 Lazarus,它会调用 FPC,并提供 LCL、项目管理、调试和表单设计器。安装资料见 Lazarus setup。

FPC 的开发分支和 snapshot 政策见 官方开发说明。部分工具不喜欢带空格的安装路径;在 Windows 上使用类似 C:FPC 的简单路径可减少兼容性问题,但这不是所有版本必然失败的规则。

编译并运行第一个程序

  1. 创建文件 hello.pas:
program Hello;

begin
  WriteLn('Hello, Pascal!');
end.
  1. 在该目录打开终端并编译:fpc hello.pas。
  2. Unix-like 系统运行 ./hello;Windows 通常运行 hello.exe。

编译通常会生成可执行文件和目标文件(如 .o),具体取决于平台和选项。不要把中间文件误认为源代码;清理项目时保留 .pas 和需要的单元文件。

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Pascal 程序的基本结构

program Demo;

uses
  SysUtils;

const
  MaxItems = 10;

type
  TCount = Integer;

var
  Count: TCount;

begin
  Count := 1;
  WriteLn(Count);
end.
  • program 声明程序;uses 引入单元。
  • const、type、var 分别声明常量、类型和变量。
  • begin ... end 构成语句块;程序末尾使用句号,普通语句通常以分号结束。

数据类型、表达式和控制流

常用类型

  • 序数类型:Boolean、Char、整数、枚举、子界和集合。
  • 浮点类型:Single、Real、Double、Extended、Comp、Currency。大小、精度和 ABI 可能随目标平台变化。
  • 结构化类型:静态/动态数组、记录、文件、类、对象、接口、指针和泛型容器。

字符串必须单独核对

ShortString、AnsiString、UnicodeString、WideString、UTF8String、RawByteString 和 PChar 不是同一种东西。字符串类型名称也不等于编码保证;PChar 是零结尾字符指针,不是托管字符串。跨平台程序应明确进行 UTF-8、系统代码页或其他编码之间的转换,不要假定一个字符永远占一个字节。

运算和流程

FPC 支持算术、比较、布尔、集合和字符串连接运算,也支持类型转换。复杂表达式请使用括号;短路求值和完全布尔求值可能因模式或编译器设置不同。

if Condition then
  DoSomething
else
  DoSomethingElse;

case Value of
  1: WriteLn('One');
  2: WriteLn('Two');
else
  WriteLn('Other');
end;

for I := 1 to 10 do WriteLn(I);
while Condition do DoSomething;
repeat
  DoSomething;
until Condition;

while 可能一次也不执行,repeat ... until 至少执行一次。注意嵌套 if 的 else 配对,以及 for 循环变量的使用限制。

过程、函数与参数

function Add(A, B: Integer): Integer;
begin
  Result := A + B;
end;

procedure Swap(var A, B: Integer);
var
  Temp: Integer;
begin
  Temp := A;
  A := B;
  B := Temp;
end;
  • 值参数复制输入;var 参数允许修改调用者变量;const 表示只读输入;out 不应按普通输入参数理解。
  • 开放数组、array of const、默认参数、重载、递归和函数类型都有各自规则;开放数组不等同于动态数组。
  • 函数通常通过内置结果变量 Result 返回值。

单元:把程序拆成可复用模块

unit Greeting;

interface

function MessageText: String;

implementation

function MessageText: String;
begin
  Result := 'Hello';
end;

end.

interface 暴露公共声明,implementation 保存实现;initialization 和 finalization 用于单元初始化与清理。接口区和实现区的 uses 作用不同,循环引用会导致编译问题。单元文件必须位于编译器搜索路径中,修改一个单元也可能触发依赖它的多个文件重新编译。

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Object Pascal:类、异常和泛型

常见能力包括字段、属性、构造函数、析构函数、继承、接口、类方法、虚方法、override、abstract、访问区段、泛型和运算符重载:

type
  TAnimal = class
  private
    FName: String;
  public
    constructor Create(const AName: String);
    procedure Speak; virtual;
    property Name: String read FName write FName;
  end;

Free Pascal 的 Object Pascal 与 Delphi 高度相似,却不是完全等价。类库、包格式、默认字符串、RTTI、编译器指令和平台行为都可能不同;迁移 Delphi GUI 项目还要处理 VCL 与 LCL、.dfm 与 Lazarus 资源、第三方组件及 Windows 专用 API。

编译器模式决定兼容目标

常见模式包括 {$mode objfpc}、{$mode delphi}、{$mode tp}、{$mode fpc} 和 {$mode iso}。模式会影响语法和语言行为,某些类、泛型、属性或运算符只有在相应模式下可用。不要只看文件扩展名判断 Delphi 兼容性;同时检查项目模式、编译器开关和依赖单元,并以对应版本的 Reference Guide 和 User’s Guide 为准。

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

常用命令行

fpc -iV                 # 查看版本
fpc -h                  # 查看帮助
fpc hello.pas           # 基本编译
fpc -Fu./src hello.pas  # 添加单元搜索路径
fpc -FEbin hello.pas    # 指定可执行文件输出目录
fpc -g hello.pas        # 生成调试信息
fpc -O2 hello.pas       # 使用优化级别 2

这些是常见示例;选项含义可能受 FPC 版本、目标平台和项目配置影响,完整说明请查 User’s Guide 或当前环境的 fpc -h。

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

常见错误的排查顺序

找不到单元

出现 Fatal: Can't find unit ... 时,先确认文件存在且为当前 CPU/操作系统编译,再检查搜索路径:fpc -Fu/path/to/units source.pas。同时检查项目配置是否覆盖默认路径,以及架构是否匹配。

语法或模式错误

检查是否指定了正确的 {$mode objfpc} 或 {$mode delphi},运行 fpc -iV 确认实际编译器版本,并核对最终编译命令和配置文件。不要只改动报错行而忽略模式不匹配。

乱码或字符串长度异常

确认实际字符串类型、编码转换、目标平台和 PChar 使用方式。UTF-8 文本在 Windows 代码页环境与 Linux/macOS 环境中不能假定表现相同;按字节截取也可能破坏多字节字符。

Lazarus 工程无法编译

能编译命令行程序只说明 FPC 基本可用,不代表 LCL widgetset、窗体资源、包和第三方组件已安装。应在 Lazarus 项目配置中检查这些依赖。

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

它适合谁

  • 学习 Pascal、数据结构和编译器基础。
  • 维护 Turbo Pascal 或旧 Object Pascal 代码。
  • 构建跨平台命令行工具和桌面 GUI。
  • 需要免费、可重复构建的教学或实验环境。

如果项目依赖 Delphi 专有生态、VCL/FireMonkey、商业支持或大量 Windows 专用组件,Delphi 可能更合适;产品入口是 Embarcadero Delphi,具体授权和价格应以所在地区的当前官方页面为准。只想免费学习语法时,不需要购买它。

快速书签

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

DelphiFree PascalLazarusObject PascalPascal编程语言编译器

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.