如何在调试期间强制链接器包含所需的功能? [英] How do I force the linker to include a function I need during debugging?

查看:97
本文介绍了如何在调试期间强制链接器包含所需的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常制作一些小的方法来协助调试,而实际程序中并未使用这些方法.通常,我的大多数班级都有一个AsString方法,我将其添加到手表中.我知道Delphi 2010具有可视化工具,但我仍然使用2007.

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.

请考虑以下示例:

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.

如果我将X.AsString添加为手表,则会得到链接器取消了要调用的函数TMyClass.AsString".

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?

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

ANSWER: GJ provided the best way to do it.

initialization
  exit;
  TMyClass(nil).AsString;

end.

推荐答案

sveinbringsli问:您也对单元功能有一些建议吗?"

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

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

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.

这篇关于如何在调试期间强制链接器包含所需的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆