Svein Bringsli
Svein Bringsli

Reputation: 5748

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

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.

Upvotes: 12

Views: 3864

Answers (3)

GJ.
GJ.

Reputation: 10937

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.

Upvotes: 6

Uli Gerhardt
Uli Gerhardt

Reputation: 14001

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

Upvotes: 0

GJ.
GJ.

Reputation: 10937

You can make function published.

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

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

Upvotes: 6

Related Questions