使用或不使用 DEBUG 条件定义

发布于 2024-11-25 18:32:25 字数 7121 浏览 9 评论 0原文

当使用调试构建配置编译我的项目时,我有一个想法在 FullDebugMode 中使用 FastMM4。

当我选择“调试”构建配置时,Delphi 自动将“DEBUG”设置为条件定义。因此,这段代码应该按预期工作:

unit uXTrackUpdater;

program Test;

uses
  {$IFDEF DEBUG}FastMM4,{$ENDIF}
  Forms;

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.Title := 'Test';
  Application.CreateForm(TfrmTest, frmMain);
  Application.Run;

并且它可以工作。但是,当我将构建配置更改为发布配置时。这段代码也有效!不应该吗?由于不应定义 DEBUG 条件。

那么,有人可以解释一下为什么这段代码无论构建配置如何都会执行,以及如何正确设置它(只有在使用调试配置时才会编译 FastMM4)。

编辑后

对于 FastMM,这只是一个示例。在在这里发布我的问题之前,我已经在不同的情况下对其进行了测试。如下:

program Project21;

{$APPTYPE CONSOLE}

uses
  {$IFDEF DEBUG} SysUtils; {$ENDIF}

begin
  try
    { TODO -oUser -cConsole Main : Insert code here }
  except
    on E:Exception do  //should not be found after rebuilding with RELEASE build configuration
      Writeln(E.Classname, ': ', E.Message);
  end;
end.

上面的代码,无论构建配置如何,都会编译。为什么?

第二次编辑后

我知道,其他人说我的示例按预期工作。但他们不为我工作。我已经在 Delphi 2010 上检查了此行为,它是相同的(仍然不起作用)。

也许这会有所帮助:我已将 DEBUG 指令替换为 RELEASE 来看看会发生什么。结果是 RELEASE 导致我的示例代码无法在两种配置中构建

第三次编辑:

我的 dproj 文件:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <ProjectGuid>{E9F0DC1F-23EC-46AA-97F8-68C007E77218}</ProjectGuid>
        <ProjectVersion>12.0</ProjectVersion>
        <MainSource>Project21.dpr</MainSource>
        <Config Condition="'$(Config)'==''">Debug</Config>
        <DCC_DCCCompiler>DCC32</DCC_DCCCompiler>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Config)'=='Base' or '$(Base)'!=''">
        <Base>true</Base>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Config)'=='Release' or '$(Cfg_1)'!=''">
        <Cfg_1>true</Cfg_1>
        <CfgParent>Base</CfgParent>
        <Base>true</Base>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Config)'=='Debug' or '$(Cfg_2)'!=''">
        <Cfg_2>true</Cfg_2>
        <CfgParent>Base</CfgParent>
        <Base>true</Base>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Base)'!=''">
        <DCC_DependencyCheckOutputName>Project21.exe</DCC_DependencyCheckOutputName>
        <DCC_ImageBase>00400000</DCC_ImageBase>
        <DCC_UnitAlias>WinTypes=Windows;WinProcs=Windows;DbiTypes=BDE;DbiProcs=BDE;DbiErrs=BDE;$(DCC_UnitAlias)</DCC_UnitAlias>
        <DCC_Platform>x86</DCC_Platform>
        <DCC_E>false</DCC_E>
        <DCC_N>false</DCC_N>
        <DCC_S>false</DCC_S>
        <DCC_F>false</DCC_F>
        <DCC_K>false</DCC_K>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Cfg_1)'!=''">
        <DCC_LocalDebugSymbols>false</DCC_LocalDebugSymbols>
        <DCC_Define>RELEASE;$(DCC_Define)</DCC_Define>
        <DCC_SymbolReferenceInfo>0</DCC_SymbolReferenceInfo>
        <DCC_DebugInformation>false</DCC_DebugInformation>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Cfg_2)'!=''">
        <DCC_Define>DEBUG;$(DCC_Define)</DCC_Define>
    </PropertyGroup>
    <ItemGroup>
        <DelphiCompile Include="Project21.dpr">
            <MainSource>MainSource</MainSource>
        </DelphiCompile>
        <BuildConfiguration Include="Base">
            <Key>Base</Key>
        </BuildConfiguration>
        <BuildConfiguration Include="Debug">
            <Key>Cfg_2</Key>
            <CfgParent>Base</CfgParent>
        </BuildConfiguration>
        <BuildConfiguration Include="Release">
            <Key>Cfg_1</Key>
            <CfgParent>Base</CfgParent>
        </BuildConfiguration>
    </ItemGroup>
    <Import Project="$(BDS)\Bin\CodeGear.Delphi.Targets" Condition="Exists('$(BDS)\Bin\CodeGear.Delphi.Targets')"/>
    <ProjectExtensions>
        <Borland.Personality>Delphi.Personality.12</Borland.Personality>
        <Borland.ProjectType/>
        <BorlandProject>
            <Delphi.Personality>
                <Source>
                    <Source Name="MainSource">Project21.dpr</Source>
                </Source>
                <Parameters>
                    <Parameters Name="UseLauncher">False</Parameters>
                    <Parameters Name="LoadAllSymbols">True</Parameters>
                    <Parameters Name="LoadUnspecifiedSymbols">False</Parameters>
                </Parameters>
                <VersionInfo>
                    <VersionInfo Name="IncludeVerInfo">False</VersionInfo>
                    <VersionInfo Name="AutoIncBuild">False</VersionInfo>
                    <VersionInfo Name="MajorVer">1</VersionInfo>
                    <VersionInfo Name="MinorVer">0</VersionInfo>
                    <VersionInfo Name="Release">0</VersionInfo>
                    <VersionInfo Name="Build">0</VersionInfo>
                    <VersionInfo Name="Debug">False</VersionInfo>
                    <VersionInfo Name="PreRelease">False</VersionInfo>
                    <VersionInfo Name="Special">False</VersionInfo>
                    <VersionInfo Name="Private">False</VersionInfo>
                    <VersionInfo Name="DLL">False</VersionInfo>
                    <VersionInfo Name="Locale">1045</VersionInfo>
                    <VersionInfo Name="CodePage">1250</VersionInfo>
                </VersionInfo>
                <VersionInfoKeys>
                    <VersionInfoKeys Name="CompanyName"/>
                    <VersionInfoKeys Name="FileDescription"/>
                    <VersionInfoKeys Name="FileVersion">1.0.0.0</VersionInfoKeys>
                    <VersionInfoKeys Name="InternalName"/>
                    <VersionInfoKeys Name="LegalCopyright"/>
                    <VersionInfoKeys Name="LegalTrademarks"/>
                    <VersionInfoKeys Name="OriginalFilename"/>
                    <VersionInfoKeys Name="ProductName"/>
                    <VersionInfoKeys Name="ProductVersion">1.0.0.0</VersionInfoKeys>
                    <VersionInfoKeys Name="Comments"/>
                </VersionInfoKeys>
            </Delphi.Personality>
        </BorlandProject>
        <ProjectFileVersion>12</ProjectFileVersion>
    </ProjectExtensions>
</Project>

谢谢。

I had an idea to use FastMM4 in FullDebugMode when compiling my project with Debug Build Configuration.

When I chose the Debug build configuration the Delphi automatically sets up DEBUG as a conditional define. So, this code should work as expected:

unit uXTrackUpdater;

program Test;

uses
  {$IFDEF DEBUG}FastMM4,{$ENDIF}
  Forms;

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.Title := 'Test';
  Application.CreateForm(TfrmTest, frmMain);
  Application.Run;

And it works. However, when I change the build configuration to the Release configuration. This code also works! And it should not? Since the DEBUG conditional should not be defined.

So, could somebody explain me why this code executes regardless of the build configuration and how to setup it properly (in a way that FastMM4 would be compiled only when using debug configuration).

After Edit

With FastMM it was only an example. Before posting here my question I've tested it on different case. Here it is:

program Project21;

{$APPTYPE CONSOLE}

uses
  {$IFDEF DEBUG} SysUtils; {$ENDIF}

begin
  try
    { TODO -oUser -cConsole Main : Insert code here }
  except
    on E:Exception do  //should not be found after rebuilding with RELEASE build configuration
      Writeln(E.Classname, ': ', E.Message);
  end;
end.

The code above, will compile no matter of build configuration. Why?

After second edit

I know, that others saying that my examples are working as expected. But they are not working for me. I've checked this behaviour on Delphi 2010 and it is identical (still not working).

Maybe this will be helpful: I've replaced DEBUG directive with RELEASE to see what will happen. The result was that RELEASE causes that my example code will not build in both configurations.

Third edit:

My dproj file:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <ProjectGuid>{E9F0DC1F-23EC-46AA-97F8-68C007E77218}</ProjectGuid>
        <ProjectVersion>12.0</ProjectVersion>
        <MainSource>Project21.dpr</MainSource>
        <Config Condition="'$(Config)'==''">Debug</Config>
        <DCC_DCCCompiler>DCC32</DCC_DCCCompiler>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Config)'=='Base' or '$(Base)'!=''">
        <Base>true</Base>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Config)'=='Release' or '$(Cfg_1)'!=''">
        <Cfg_1>true</Cfg_1>
        <CfgParent>Base</CfgParent>
        <Base>true</Base>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Config)'=='Debug' or '$(Cfg_2)'!=''">
        <Cfg_2>true</Cfg_2>
        <CfgParent>Base</CfgParent>
        <Base>true</Base>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Base)'!=''">
        <DCC_DependencyCheckOutputName>Project21.exe</DCC_DependencyCheckOutputName>
        <DCC_ImageBase>00400000</DCC_ImageBase>
        <DCC_UnitAlias>WinTypes=Windows;WinProcs=Windows;DbiTypes=BDE;DbiProcs=BDE;DbiErrs=BDE;$(DCC_UnitAlias)</DCC_UnitAlias>
        <DCC_Platform>x86</DCC_Platform>
        <DCC_E>false</DCC_E>
        <DCC_N>false</DCC_N>
        <DCC_S>false</DCC_S>
        <DCC_F>false</DCC_F>
        <DCC_K>false</DCC_K>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Cfg_1)'!=''">
        <DCC_LocalDebugSymbols>false</DCC_LocalDebugSymbols>
        <DCC_Define>RELEASE;$(DCC_Define)</DCC_Define>
        <DCC_SymbolReferenceInfo>0</DCC_SymbolReferenceInfo>
        <DCC_DebugInformation>false</DCC_DebugInformation>
    </PropertyGroup>
    <PropertyGroup Condition="'$(Cfg_2)'!=''">
        <DCC_Define>DEBUG;$(DCC_Define)</DCC_Define>
    </PropertyGroup>
    <ItemGroup>
        <DelphiCompile Include="Project21.dpr">
            <MainSource>MainSource</MainSource>
        </DelphiCompile>
        <BuildConfiguration Include="Base">
            <Key>Base</Key>
        </BuildConfiguration>
        <BuildConfiguration Include="Debug">
            <Key>Cfg_2</Key>
            <CfgParent>Base</CfgParent>
        </BuildConfiguration>
        <BuildConfiguration Include="Release">
            <Key>Cfg_1</Key>
            <CfgParent>Base</CfgParent>
        </BuildConfiguration>
    </ItemGroup>
    <Import Project="$(BDS)\Bin\CodeGear.Delphi.Targets" Condition="Exists('$(BDS)\Bin\CodeGear.Delphi.Targets')"/>
    <ProjectExtensions>
        <Borland.Personality>Delphi.Personality.12</Borland.Personality>
        <Borland.ProjectType/>
        <BorlandProject>
            <Delphi.Personality>
                <Source>
                    <Source Name="MainSource">Project21.dpr</Source>
                </Source>
                <Parameters>
                    <Parameters Name="UseLauncher">False</Parameters>
                    <Parameters Name="LoadAllSymbols">True</Parameters>
                    <Parameters Name="LoadUnspecifiedSymbols">False</Parameters>
                </Parameters>
                <VersionInfo>
                    <VersionInfo Name="IncludeVerInfo">False</VersionInfo>
                    <VersionInfo Name="AutoIncBuild">False</VersionInfo>
                    <VersionInfo Name="MajorVer">1</VersionInfo>
                    <VersionInfo Name="MinorVer">0</VersionInfo>
                    <VersionInfo Name="Release">0</VersionInfo>
                    <VersionInfo Name="Build">0</VersionInfo>
                    <VersionInfo Name="Debug">False</VersionInfo>
                    <VersionInfo Name="PreRelease">False</VersionInfo>
                    <VersionInfo Name="Special">False</VersionInfo>
                    <VersionInfo Name="Private">False</VersionInfo>
                    <VersionInfo Name="DLL">False</VersionInfo>
                    <VersionInfo Name="Locale">1045</VersionInfo>
                    <VersionInfo Name="CodePage">1250</VersionInfo>
                </VersionInfo>
                <VersionInfoKeys>
                    <VersionInfoKeys Name="CompanyName"/>
                    <VersionInfoKeys Name="FileDescription"/>
                    <VersionInfoKeys Name="FileVersion">1.0.0.0</VersionInfoKeys>
                    <VersionInfoKeys Name="InternalName"/>
                    <VersionInfoKeys Name="LegalCopyright"/>
                    <VersionInfoKeys Name="LegalTrademarks"/>
                    <VersionInfoKeys Name="OriginalFilename"/>
                    <VersionInfoKeys Name="ProductName"/>
                    <VersionInfoKeys Name="ProductVersion">1.0.0.0</VersionInfoKeys>
                    <VersionInfoKeys Name="Comments"/>
                </VersionInfoKeys>
            </Delphi.Personality>
        </BorlandProject>
        <ProjectFileVersion>12</ProjectFileVersion>
    </ProjectExtensions>
</Project>

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄风 2024-12-02 18:32:25

使用条件编译时,请确保始终在切换配置后进行构建。显然,当前的“定义”集不是用于测试某个单元是否需要重新编译的密钥的一部分。如果您更改的只是定义(即:DEBUG 不再定义),则编译器根本不知道它,并继续使用预编译单元。

但由于这是关于 FastMM4 的,所以还有另一种方法:您可以为 DEBUG 和 RELEASE 定义InstallOnlyIfRunningInIDE,并且 FastMM4 不会安装它的内存管理器,除非您从 IDE 运行。


编辑

鉴于您后来的编辑,我认为您的项目配置是罪魁祸首,因为 IFDEF 工作得很好。检查配置选项的两个分支,确保没有为发布和调试定义“DEBUG”。

When using conditional compilation, make sure you always do a build after switching configurations. Apparently the current set of "defines" is not part of the key that's used to test if a certain unit needs recompilation or not. If all you changed is a define (ie: DEBUG is no longer defined), the compiler simply doesn't know about it, and keeps using the pre-compiled unit.

But since this is about FastMM4, there's an other way: You can define InstallOnlyIfRunningInIDE for both DEBUG and RELEASE and FastMM4 will not install it's memory manager unless you're running from the IDE.


Edit

Given your later edit, I assume your project's configuration is to blame, because IFDEF's work just fine. Inspect both branches of your configuration options, very that you don't have "DEBUG" defined for both Release and Debug.

小忆控 2024-12-02 18:32:25

似乎 DEBUG 是在基本配置上定义的(应该具有通用的编译设置)。当您更改 Base 时,所有未修改的设置默认为 Base 值。

验证您是否无意中将 DEBUG 放在 Base 配置而不是 Debug 配置上。

Appears that DEBUG is defined on Base configuration (which is supposed to have the common compilation settings). When you change Base, all non-modified settings default to Base values.

Verify if you doesn't put DEBUG on Base configuration instead of Debug config by accident.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文