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

13,087

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.

Share:
13,087
magnus
Author by

magnus

Updated on June 05, 2022

Comments

  • magnus
    magnus almost 2 years

    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?

  • Free Consulting
    Free Consulting about 10 years
    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).
  • David Heffernan
    David Heffernan about 10 years
    @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.
  • skamradt
    skamradt about 10 years
    @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.
  • Free Consulting
    Free Consulting about 10 years
    @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.
  • LU RD
    LU RD over 3 years
    This answer does not in any way try to answer what was asked.