Lazarus Pascal过程式“单元”的语法规则

6

我使用“文件 -> 新建单元”将我的应用程序源代码组织成Pascal编译单元。

以下单元可以成功编译...

unit CryptoUnit;

{$mode objfpc}{$H+}

interface
  function Encrypt(key, plaintext:string):string;
  function Decrypt(key, ciphertext:string):string;

implementation

uses
  Classes, SysUtils, Blowfish;

function Encrypt(key, plaintext:string):string; 
...

然而,这个代码存在编译错误,因为它无法在第6行识别“Exception”。
unit ExceptionUnit;

{$mode objfpc}{$H+}

interface
  procedure DumpExceptionCallStack(E: Exception);  // <--- problem

implementation

uses
  Classes, SysUtils, FileUtil;


{ See http://wiki.freepascal.org/Logging_exceptions }

procedure DumpExceptionCallStack(E: Exception);      
...

如果我假设ExceptionSysUtils中定义(我该如何判断?),那么我无法在interface之前添加uses SysUtils(编译器会报错,因为它期望看到interface)。

我该如何告诉编译器ExceptionSysUtils中定义?


我认为你需要将 uses SysUtils 行放在 interface 行之后(即不要放在它之前)。 - Paul R
1个回答

6

在接口关键字之后,但在接口部分中的其他语句之前,应引用您的单元使用的其他单元。

您的示例应该以以下形式工作:

unit ExceptionUnit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil;

procedure DumpExceptionCallStack(E: Exception);

implementation

{ See http://wiki.freepascal.org/Logging_exceptions }

procedure DumpExceptionCallStack(E: Exception); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接