문제

I would like to show in the Caption of the program how was the program complied. Most important, I would like to show if the Compiler Optimization is on/off.

(Range Checking and other stuff like this would be interesting to show also, but mainly I am interested in Compiler Optimization).

Any idea how to do it?


Ready to use function based on Arioch answer:

function CompilerOptimization: Boolean;  { Importan note: $O+ has a local scope, therefore, the result of the function reflects only the optimization state at that specific source code location. }
begin
 {$IfOpt O+}
 Result:= TRUE;
 {$Else}
 Result:= FALSE;
 {$EndIf}
end;

function CompilerOptimizationS: String;
begin
 Result:= 'Compiler optimization is ' +
 {$IfOpt O+}
 'enabled'
 {$Else}
 'disabled'
 {$EndIf}
end;

IMPORTANT: If you are using the {$O} switch to optimize pieces of code then it MUST be used as a subfunction like this, otherwise, if you use the global switch only (in Project Options) it can be used as a normal (declared) function.

// {$O+} or {$O-}
procedure TFrmTest.FormCreate(Sender: TObject);

 function CompilerOptimizationS: String;
 begin
  Result:= 'Compiler optimization is ' +
  {$IfOpt O+}
  'enabled'
  {$Else}
  'disabled'
  {$EndIf}
 end;

begin
 ///...more code here
 Caption:= 'Version: '+ GerVerStr+ ' '+ CompilerOptimizationS+ etc+ etc;
end;
도움이 되었습니까?

해결책

ShowMessage(' Optimization is ' +
{$IfOpt O+}
'enabled'
{$Else}
'disabled'
{$EndIf}
);

PS. Documentation... Going Google for "Delphi IfOpt" will bring you a lot of links, including

PPS. gammatester is absolutely correct that "Since {$O+} has a local scope, your message/caption reflects only the optimization state at that specific source code location"

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top