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?

Community
  • 1
  • 1
magnus
  • 4,031
  • 7
  • 26
  • 48
  • 1
    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 http://stackoverflow.com/questions/176159/in-delphi-7-can-i-set-up-debug-and-release-modes. – Graymatter Mar 12 '14 at 02:53
  • 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? – Ken White Mar 12 '14 at 02:56
  • 4
    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 units – Sertac Akyuz Mar 12 '14 at 02:58
  • I don't believe D7 has build configurations, but maybe I'm just not seeing it. – magnus Mar 12 '14 at 03:02
  • 3
    @user - D7 does not have build configurations. Read past the first sentence of the first comment. – Sertac Akyuz Mar 12 '14 at 03:04
  • @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. – Free Consulting Mar 12 '14 at 05:31
  • 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. – Stijn Sanders Mar 12 '14 at 10:13
  • For info: I have tested the code in Delphi Berlin 10.1, it works correctly. – Pax Beach Jan 19 '17 at 05:51

3 Answers3

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.

skamradt
  • 15,366
  • 2
  • 36
  • 53
  • 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). – Free Consulting Mar 12 '14 at 05:41
  • 1
    @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. – David Heffernan Mar 12 '14 at 07:23
  • @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. – skamradt Mar 12 '14 at 15:27
  • @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. – Free Consulting Mar 13 '14 at 02:18
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.

Jon
  • 812
  • 2
  • 11
  • 18
-2

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

Geziel
  • 1