Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi {$IFDEF CONSOLE} Problem

I just tried

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

begin
  {$IFDEF CONSOLE}
    beep;
  {$ENDIF}
end.

and expected to hear a beep during runtime, but not. The following test works, though:

  if IsConsole then
    beep;

Why doesn't the compile-time test work? As far as I can understand from this doc, it sure should work.

like image 802
Andreas Rejbrand Avatar asked Jul 10 '10 10:07

Andreas Rejbrand


3 Answers

easier solution:

program YourProgram;
{$DEFINE MakeConsoleApp}
{$IFDEF MakeConsoleApp}
  {$APPTYPE CONSOLE}
{$ENDIF}



[....]
{$IFDEF MakeConsoleApp} WriteLn('Text in a Console'); {$ENDIF}

so anytime you want to make you application not show a console you would just change {$DEFINE MakeConsoleApp} to { } or {.$DEFINE MakeConsoleApp}

like image 162
tndza Avatar answered Oct 14 '22 15:10

tndza


It does not work in *.dpr file, but it is OK in a unit (call MakeBeep from console *.dpr):

unit Unit1;

interface

uses
  SysUtils;

procedure MakeBeep;

implementation

procedure MakeBeep;
begin
  {$IFDEF CONSOLE}
    beep;
  {$ENDIF}
end;
like image 33
kludg Avatar answered Oct 14 '22 17:10

kludg


If you select "Generate console application" from the linker options, 'CONSOLE' is defined.

like image 42
Sertac Akyuz Avatar answered Oct 14 '22 16:10

Sertac Akyuz