简体   繁体   English

为什么我的 {$IFDEF DEBUG} 条件在 Delphi 中不起作用?

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

I have the following code (IP addresses changed) in a Delphi 7 project.我在 Delphi 7 项目中有以下代码(IP 地址已更改)。

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 .在上面的代码示例中,没有定义“DEBUG”符号,因此将 IP 地址设置为123.456.789.12而不是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?这个问题来自Delphi 的条件编译是否允许定义的符号包含值?

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.如果您编译您的项目并且没有任何更改并且 DCU 在最后一个非调试版本的路径上可用,那么它将被使用,从而导致此问题。 Also make sure this unit is included in the uses clause of the DPR.还要确保此单元包含在 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.我通常为语法编译,但总是为测试/部署而构建。

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.在 Delphi 2010(毫无疑问是其他版本)中,DEBUG 条件由配置管理器设置,它实际上是一个保留字。

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: -您可以通过更改编译器配置来更改这些条件的设置:-

项目/配置经理

In short, don't use DEBUG or RELEASE for your own use - make up a unique directive for your testing.简而言之,不要将 DEBUG 或 RELEASE 用于您自己的用途 - 为您的测试制定一个独特的指令。

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.尽管有其他评论,我使用的条件有助于解决语法错误、较小的 exe 并防止我不想发布的代码的逆向工程。

“$IFNDEF”代替“IFDEF”(否定形式Ndef代替def)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在Delphi XE5中将{$ IFDEF MSWINDOWS}替换为{$ IF defined(MSWINDOWS)}? - Why {$IFDEF MSWINDOWS} is replaced with {$IF defined(MSWINDOWS)} in Delphi XE5? Delphi {$ IFDEF CONSOLE}问题 - Delphi {$IFDEF CONSOLE} Problem 为什么不能在设备上调试Delphi XE7 Android应用程序? - Why can't I debug a Delphi XE7 Android app on my device? Delphi:如果我在我的项目中使用包,为什么“使用调试 DCU”变得无效? - Delphi: Why does “use debug DCUs” become ineffective if I'm using packages in my project? 自动删除Delphi IFDEf编译器指令 - Removing Delphi IFDEf compiler directives automatically TaskDialog 在我的 Delphi 程序中不起作用 - TaskDialog not working in my Delphi Program 如何使用 Delphi 的 PAServer 调试我的 DLL 项目 - How can i debug my DLL project with Delphi's PAServer Delphi 2010中$ IFDEF块内的代码导航中断 - Code navigation breaks inside of $IFDEF blocks in Delphi 2010 Delphi 6中的代码语法突出显示(突出显示语法和ifdef块) - Code Syntax Highlighting in Delphi 6 (to highlight syntax and ifdef blocks) “ CDO.Message”在我的Delphi应用程序中不起作用 - “CDO.Message” is not working in my Delphi application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM