[ACCEPTED]-Delphi #IF(DEBUG) equivalent?-compiler-directives

Accepted answer
Score: 34

Use this:



Score: 8

Apart from what lassevk said, you can also 26 use a few other methods of compiler-evaluation 25 (since Delphi 6, I believe) :

  // Mind you : The NOT above is optional

To check if 24 the compiler has this feature, use :


There 23 are several uses for this.

For example, you 22 could check the version of the RTL; From 21 the Delphi help :

You can use RTLVersion 20 in $IF expressions to test the runtime library 19 version level independently of the compiler 18 version level.
Example: {$IF RTLVersion 17 >= 16.2} ... {$IFEND}

Also, the compiler 16 version itself can be checked, again from 15 the code:

CompilerVersion is assigned a value 14 by the compiler when the system unit 13 is compiled. It indicates the revision level 12 of the compiler features / language 11 syntax, which may advance independently 10 of the RTLVersion. CompilerVersion 9 can be tested in $IF expressions and should 8 be used instead of testing for the VERxxx conditional 7 define. Always test for greater than 6 or less than a known revision level. It's 5 a bad idea to test for a specific revision 4 level.

Another thing I do regularly, is define 3 a symbol when it's not defined yet (nice 2 for forward-compatiblity), like this :

   UTF8String = type AnsiString;

Hope 1 this helps!

Score: 8

DebugHook is set if an application is running 3 under the IDE debugger. Not the same as 2 a compiler directive but still pretty useful. For 1 example:

ReportMemoryLeaksOnShutdown := DebugHook <> 0; // show memory leaks when debugging
Score: 3

These control directives are available:

{$IFNDEF} //if *not* defined

and 1 they can be used as shown here:

procedure TfrmMain.Button1Click(Sender: TObject);
  ShowMessage('my conditional IS defined!');
  ShowMessage('my conditional is NOT defined!');

  ShowMessage('My conditional is explicitly NOT defined');

More Related questions