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?

RedGrittyBrick
  • 3,827
  • 1
  • 30
  • 51
  • I think you need to put the `uses SysUtils` line immediately after the `interface` line (i.e. not before it). – Paul R Aug 23 '13 at 17:01

1 Answers1

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); 
jwdietrich
  • 474
  • 6
  • 17