如何强制链接器在调试期间包含我需要的函数?

Sve*_*sli 12 delphi debugging linker

我经常使用小方法来协助调试,这些方法在实际程序中没有使用.通常,我的大多数类都有一个AsString方法,我将其添加到手表中.我知道Delphi 2010有可视化工具,但我还在2007年.

考虑这个例子:

program Project1;

{$APPTYPE CONSOLE}

uses SysUtils;

type
  TMyClass = class
    F : integer;
    function AsString : string;
  end;

function TMyClass.AsString: string;
begin
  Result := 'Test: '+IntToStr(F);
end;

function SomeTest(aMC : TMyClass) : boolean;
begin
  //I want to be able to watch aMC.AsString while debugging this complex routine!
  Result := aMC.F > 100; 
end;

var
  X : TMyClass;

begin
  X := TMyClass.Create;
  try
    X.F := 100;
    if SomeTest(X)
      then writeln('OK')
      else writeln('Fail');
  finally
    X.Free;
  end;
  readln;
end.
Run Code Online (Sandbox Code Playgroud)

如果我将X.AsString作为监视添加,我只是得到"要调用的函数,TMyClass.AsString,被链接器删除".

如何强制链接器包含它?我通常的诀窍是在程序的某个地方使用该方法,但是没有更优雅的方法吗?

答案:GJ提供了最好的方法.

initialization
  exit;
  TMyClass(nil).AsString;

end.
Run Code Online (Sandbox Code Playgroud)

GJ.*_*GJ. 6

你可以发布功能.

  TMyClass = class
    F : integer;
  published
    function AsString : string;
  end;
Run Code Online (Sandbox Code Playgroud)

然后在'Watch Properties''允许函数调用'中打开


GJ.*_*GJ. 6

sveinbringsli问:"你有单位功能的小费吗?"

Delphi编译器很聪明......所以你可以做类似......

unit UnitA;

interface

{$DEFINE DEBUG}

function AsString: string;

implementation

function AsString: string;
begin
  Result := 'Test: ';
end;

{$IFDEF DEBUG}
initialization
  exit;
  AsString;
{$ENDIF}
end.
Run Code Online (Sandbox Code Playgroud)