How do I force the linker to include a function I need during debugging?

故事扮演 提交于 2019-12-30 06:08:45

问题


I often make small methods to assist debugging, which aren't used in the actual program. Typically most of my classes has an AsString-method which I add to the watches. I know Delphi 2010 has visualizers, but I'm still on 2007.

Consider this example:

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.

If I add X.AsString as a watch, I just get "Function to be called, TMyClass.AsString, was eliminated by linker".

How do I force the linker to include it? My usual trick is to use the method somewhere in the program, but isn't there a more elegant way to do it?

ANSWER: GJ provided the best way to do it.

initialization
  exit;
  TMyClass(nil).AsString;

end.

回答1:


sveinbringsli ask: "Do you have a tip for unit functions also?"

Delphi compiler is smart... So you can do something like...

unit UnitA;

interface

{$DEFINE DEBUG}

function AsString: string;

implementation

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

{$IFDEF DEBUG}
initialization
  exit;
  AsString;
{$ENDIF}
end.



回答2:


You can make function published.

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

And switch on in 'Watch Properties' 'Allow function calls'




回答3:


Maybe it works to call them in some initialization section, guarded by {IFDEF DEBUG} or {IFOPT D+}.



来源:https://stackoverflow.com/questions/1606105/how-do-i-force-the-linker-to-include-a-function-i-need-during-debugging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!