Syntax rules for Lazarus Pascal procedural "Units"
Asked Answered
B

1

6

I organise my app's source code into Pascal compilation Units using File -> New Unit

The following Unit compiles OK ...

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

However this one has compilation errors as it can't identify "Exception" at line 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);      
...

If I assume that Exception is defined in SysUtils (how can I tell?) I can't put uses SysUtils before interface (the compiler complains it was expecting interface)

How do I tell the compiler that Exception is defined in SysUtils?

Berenice answered 23/8, 2013 at 16:52 Comment(1)
I think you need to put the uses SysUtils line immediately after the interface line (i.e. not before it).Laminitis
T
6

Other units that are used used by your unit are to be referenced after the interface keyword, but before other statements in the interface section.

Your example should work in the following form:

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); 
Trefoil answered 23/8, 2013 at 20:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.