[英]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 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.