Why is my {$IFDEF DEBUG} conditional not working in Delphi?
Asked Answered
C

3

5

I have the following code (IP addresses changed) in a Delphi 7 project.

const
{$IFNDEF DEBUG}
    AUTHENTICATOR_DB_ADMIN_HOST = '123.456.789.12';
{$ELSE}
    AUTHENTICATOR_DB_ADMIN_HOST = '127.0.0.1';
{$ENDIF}

Under project options:

  • In the "Directories/Conditionals" tab under the section marked "Conditionals", I have a single definition: "DEBUG".
  • In the "Version Info" tab under the section marked "Module Attributes", I have ticked the checkbox labelled "Debug-build".

In the above code example, the "DEBUG" symbol is not defined, so the IP address is set to 123.456.789.12 instead of 127.0.0.1. What am I doing wrong?

This question is following on from Does Delphi's conditional compilation allow the defined symbols to contain values?

Carnarvon answered 12/3, 2014 at 2:52 Comment(8)
Is your Build Configuration in the project manager set to debug? I just picked up that it's Delphi 7. You can look at this question for switching in Delphi 7 #176659.Geminate
Works fine for me on Delphi 2007 and XE5. I don't remember if Delphi 7 supported build configurations or not; if it does, are you using the debug configuration?Incomprehensive
Version info does not matter. Conditional define in project options is enough. Build your project just in case. You can also insert {$DEFINE DEBUG} to have it defined only in specific unitsAntedate
I don't believe D7 has build configurations, but maybe I'm just not seeing it.Carnarvon
@user - D7 does not have build configurations. Read past the first sentence of the first comment.Antedate
@user1420752, Delphi 7 hasn't. You can bring a similar tool in by IDE add-in. But anyway, it does not matter for your problem. Just ensure what your conditional symbol takes an effect by rebuilding any dependent modules.Cage
Just in case everybody is missing the elephant in the room: things like this should not go hard-coded in the source-code, even with a compiler-directive-switch. More suitable would be the registry or an ini-file, optionally encrypted to protect it from tampering.Cardiogram
For info: I have tested the code in Delphi Berlin 10.1, it works correctly.Bleeder
T
9

If you compile your project and there are no changes and the DCU is available on the path for the last non debug build then it will be used, causing this problem. Also make sure this unit is included in the uses clause of the DPR.

If you build the project it will force a recompile of all units added to the project.

I generally compile for syntax but always build for testing/deployment.

Thrasher answered 12/3, 2014 at 4:24 Comment(4)
Yes, but actually DPR uses clause isn't necessary. When in make mode (-m, default) compiler checks if DCU is up to date by comparing FileAge(sourcefile) and FileAge(dcubinary). Therefore compiler will not sense any changes made to conditionals (-dSYM switch).Cage
@Free Compiler has to check more than that. It has to re-compile any units that depend on units that have interface modifications since dcu was made.Slambang
@Free, true but I generally will add the unit to the DPR if it will need to be recompiled as part of the project, it also helps avoid the stray dcu problem, where a dcu compiled maybe from another project ends up conflicting with what I think I'm building.Thrasher
@DavidHeffernan, yes, but AFAIK DCU object have no record about conditional symbols of its compilation. So, there is still no way to tell resulting object files of dcc -dFOO faux.pas and dcc -dBAR faux.pas apart, assuming they both are up to date.Cage
D
0

Old question I know, but here is the answer for me. In Delphi 2010 (an no doubt others) the DEBUG condition is set by the Configuration Manager, it's a Reserved Word as it were.

Consider this trivial example: -

program Buggy;

{$APPTYPE CONSOLE}

uses
  SysUtils;

begin
{$IFDEF DEBUG}
  WriteLn('DEBUG condition is ON.');
{$ELSE}
  WriteLn('DEBUG condition is OFF.');
{$ENDIF}
{$IFDEF RELEASE}
  WriteLn('RELEASE condition is ON.');
{$ELSE}
  WriteLn('RELEASE condition is OFF.');
{$ENDIF}
  ReadLn;
end.

You can change the setting of these conditions by changing the compiler configuration: -

Project / Configuration Manager

In short, don't use DEBUG or RELEASE for your own use - make up a unique directive for your testing.

Despite other comments I use conditions which helps with syntax errors, smaller exe's and prevents reverse engineering of code I don't want released.

Dix answered 5/10, 2020 at 23:16 Comment(0)
S
-1

"$IFNDEF" instead "IFDEF" (The negative form Ndef instead def).

Studio answered 3/12, 2020 at 21:27 Comment(2)
This answer does not in any way try to answer what was asked.Elah
Perhaps not, but that this point this page is google's top result for "delphi ifdef not" so it helped me out.Consignment

© 2022 - 2024 — McMap. All rights reserved.