Lazarus Pascal程序“单位”的语法规则

时间:2013-08-23 16:52:41

标签: syntax lazarus

我使用File -> New Unit

将我的应用源代码整理到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行识别“异常”......

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);      
...

如果我假设在Exception中定义了SysUtils(我怎么知道?)我不能在uses SysUtils之前放置interface(编译器抱怨它期待interface

如何告诉编译器Exception中定义了SysUtils

1 个答案:

答案 0 :(得分:6)

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

您的示例应采用以下形式:

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);