23

Is there a Delphi equivalent of the C# #if(DEBUG) compiler directive?

Boris Callens
  • 90,659
  • 85
  • 207
  • 305

4 Answers4

37

Use this:

{$IFDEF DEBUG}
...
{$ENDIF}
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • And make sure the project options define DEBUG. I think only the newer Delphi (D2007 and up?) versions set it by default for a debug build. – Lars Truijens Sep 29 '08 at 07:49
  • 5
    For older versions you can use {$IFOPT D+}. 'D' is the compiler option symbol for "Compile with Debug Info". 'D+' indicates that what follows should only be compiled if that option is *ON*. – Deltics Jan 09 '10 at 21:52
  • 1
    For portability of the more readable {$IFDEF DEBUG} you could of course include something like {$IFOPT C+}{$DEFINE DEBUG}{$ENDIF} in a standard defines include file (in the sections relating to versions of Delphi that don't auto-define DEBUG for you. Note however that this "auto-define" is not always reliable. DEBUGINFO I think is auto-defined but DEBUG is only defined if you leave the default conditionals for the DEBUG build as they are (they include DEBUG as a "factory" setting, but this can and may be removed. i.e. It's not a "built-in" definition when building with Debug Info on. – Deltics Jan 09 '10 at 21:56
  • 1
    @Deltics: IFOPT C+ is true when assertions are on, do you mean IFOPT D+? – David Heffernan Dec 19 '10 at 12:11
  • @Deltics: thats antincorrect, $IFDEF DEBUG is with us since ~1990 – Free Consulting Dec 20 '10 at 03:16
  • 1
    @user205376 - Set project options to "Debug", then remove the DEBUG conditional define. Or, turn on "Debug Info" but *fail* to add a DEBUG conditional define. Either way, the result is: A build with debug info but code enclosed in $ifdef DEBUG will NOT be compiled. The convention of adding a default define of a DEBUG compiler conditional symbol may have been with us since 1990 (but Delphi only since 1995, so how/if/why it perpetuates the convention is entirely up for grabs and clearly the presence of a DEBUG define is NOT connected to the presence of Debug Information in the build). – Deltics Dec 20 '10 at 08:49
  • @David - yes. my bad. I guess the mistake was easily spotted tho :) – Deltics Dec 20 '10 at 08:50
9

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

ReportMemoryLeaksOnShutdown := DebugHook <> 0; // show memory leaks when debugging
Lawrence Barsanti
  • 31,929
  • 10
  • 46
  • 68
8

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

{$IF NOT DECLARED(SOME_SYMBOL)} 
  // Mind you : The NOT above is optional
{$ELSE}
{$IFEND}

To check if the compiler has this feature, use :

 {$IFDEF CONDITIONALEXPRESSIONS}

There are several uses for this.

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

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

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

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

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

 {$IF NOT DECLARED(UTF8String)}
 type
   UTF8String = type AnsiString;
 {$IFEND} 

Hope this helps!

Ritsaert Hornstra
  • 5,013
  • 1
  • 33
  • 51
PatrickvL
  • 4,104
  • 2
  • 29
  • 45
3

These control directives are available:

{$IFDEF}
{$ELSE}
{$ENDIF}
{$IFNDEF} //if *not* defined

and they can be used as shown here:

procedure TfrmMain.Button1Click(Sender: TObject);
begin
  {$IFDEF MY_CONDITIONAL}
  ShowMessage('my conditional IS defined!');
  {$ELSE}
  ShowMessage('my conditional is NOT defined!');
  {$ENDIF}

  {$IFNDEF MY_CONDITIONAL}
  ShowMessage('My conditional is explicitly NOT defined');
  {$ENDIF}
end;
JosephStyons
  • 57,317
  • 63
  • 160
  • 234
  • 3
    You forgot {$IFOPT}, which can be used to directly test for compiler settings. {$IFOPT D+} // DEBUG INFO ON {$IFOPT C+} // ASSERTIONS ENABLED etc – Deltics Jan 09 '10 at 21:57