Recent

Author Topic: [SOLVED]How to detect if a compiler option is enabled or not in code  (Read 1575 times)

BeanzMaster

  • Sr. Member
  • ****
  • Posts: 268
Hi to all,

All is in in the title, i would like to detect in one of my unit if -WG is enabled or not. It is possible ? if yes how

Thanks in advance

Best regards
« Last Edit: February 11, 2020, 06:19:45 pm by BeanzMaster »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to detect if a compiler option is enabled or not in code
« Reply #1 on: February 11, 2020, 05:13:23 pm »
IIRC it can only be done with options which have an equivalent short directive. For example with -Sh (equivalent to {$H+}), you can use {$IFOPT H+}.

Also, most cmd. line options set or reset features which you can interrogate in code with {$ifdef FPC_HAS_XXX} and similar.

Note that, specifically for -GW, you can detect whether your program is a GUI or console one by interrogating Application.ConsoleApplication, which in turns interrogates System.IsConsole.

« Last Edit: February 11, 2020, 05:28:34 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

BeanzMaster

  • Sr. Member
  • ****
  • Posts: 268
Re: How to detect if a compiler option is enabled or not in code
« Reply #2 on: February 11, 2020, 06:19:13 pm »
Ok thanks so it's not possible in my case   :'(I have a gui application and i'm using my own logger (threaded and multiple output support like file, modal form and directly in the console) and It would have been nice if i could detect this option to automatically enable log output to the console.  i can only detect the D+ options but is not efficient in my case

So, btw,, i'll do Enabled/Disabled my console log output manually :(

Thanks Lucamar

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: How to detect if a compiler option is enabled or not in code
« Reply #3 on: February 11, 2020, 06:35:45 pm »
Ok thanks so it's not possible in my case   :'(I have a gui application and i'm using my own logger (threaded and multiple output support like file, modal form and directly in the console) and It would have been nice if i could detect this option to automatically enable log output to the console.  i can only detect the D+ options but is not efficient in my case

So, btw,, i'll do Enabled/Disabled my console log output manually :(

Thanks Lucamar
Code: Pascal  [Select][+][-]
  1. procedure IsConsoleWindowActive;
  2. begin
  3.   showmessage(BoolToStr(IsConsole, 'Console window is open', 'No Console found'));
  4. end;
  5.  

ezlage

  • Guest
Re: [SOLVED]How to detect if a compiler option is enabled or not in code
« Reply #4 on: February 11, 2020, 09:09:17 pm »
There is a workaround. I tested and it worked for me. With "-WG" you will receive "false" as response, without "-WG" you will receive "true".

If you compile a non-console application with this code, the debugger will always raise a RunError(103) and an EInOutError, but you can just continue or ignore these exception types.

Code: Pascal  [Select][+][-]
  1. var
  2.   isConsole: boolean;
  3. begin
  4.   try
  5.     writeln;
  6.     isConsole:=True;
  7.   finally
  8.     ShowMessage('Is console? '+BoolToStr(isConsole,True));
  9.   end;
  10.   //[...]
  11. end.

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: [SOLVED]How to detect if a compiler option is enabled or not in code
« Reply #5 on: February 11, 2020, 10:36:36 pm »
There is a workaround. I tested and it worked for me. With "-WG" you will receive "false" as response, without "-WG" you will receive "true".

If you compile a non-console application with this code, the debugger will always raise a RunError(103) and an EInOutError, but you can just continue or ignore these exception types.

Code: Pascal  [Select][+][-]
  1. var
  2.   isConsole: boolean;
  3. begin
  4.   try
  5.     writeln;
  6.     isConsole:=True;
  7.   finally
  8.     ShowMessage('Is console? '+BoolToStr(isConsole,True));
  9.   end;
  10.   //[...]
  11. end.
IsConsole is a public cross platform variable defined in the system unit and already set properly no need to redefine it limiting your self in OS specific parameters.

ezlage

  • Guest
Re: [SOLVED]How to detect if a compiler option is enabled or not in code
« Reply #6 on: February 11, 2020, 10:48:24 pm »
There is a workaround. I tested and it worked for me. With "-WG" you will receive "false" as response, without "-WG" you will receive "true".

If you compile a non-console application with this code, the debugger will always raise a RunError(103) and an EInOutError, but you can just continue or ignore these exception types.

Code: Pascal  [Select][+][-]
  1. var
  2.   isConsole: boolean;
  3. begin
  4.   try
  5.     writeln;
  6.     isConsole:=True;
  7.   finally
  8.     ShowMessage('Is console? '+BoolToStr(isConsole,True));
  9.   end;
  10.   //[...]
  11. end.
IsConsole is a public cross platform variable defined in the system unit and already set properly no need to redefine it limiting your self in OS specific parameters.

Nice to know. Thanks!

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: [SOLVED]How to detect if a compiler option is enabled or not in code
« Reply #7 on: February 12, 2020, 01:06:41 am »
IsConsole is a public cross platform variable defined in the system unit and already set properly no need to redefine it limiting your self in OS specific parameters.

IsConsole always returns true in macOS whether running from a command line application or a GUI application.

Code: Pascal  [Select][+][-]
  1. Program ConsoleTest;
  2.  
  3. Uses sysutils;
  4.  
  5. begin
  6.  WriteLn(IsConsole);
  7.  WriteLn(BoolToStr(IsConsole));
  8. end.

Returns: TRUE and -1 as expected.

Code: Pascal  [Select][+][-]
  1. ...
  2. NSLog(NSStr(BoolToStr(IsConsole));
  3. ShowMessage('Console: ' + BoolToStr(IsConsole));
  4. ...

Returns: -1 and -1 which is not expected.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: [SOLVED]How to detect if a compiler option is enabled or not in code
« Reply #8 on: February 12, 2020, 09:02:03 am »
The *nix based platforms (Linux, macOS, FreeBSD, etc.) don't know the concept of GUI vs console application. For these all applications are always console applications.

BeanzMaster

  • Sr. Member
  • ****
  • Posts: 268
Re: [SOLVED]How to detect if a compiler option is enabled or not in code
« Reply #9 on: February 13, 2020, 05:06:34 pm »
Hi, thanks to all for informations and tips. I'll do it manually because is need multiplateform

Thanks

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: [SOLVED]How to detect if a compiler option is enabled or not in code
« Reply #10 on: February 13, 2020, 06:08:17 pm »
It is just a one liner:
Code: Pascal  [Select][+][-]
  1. {$ifdef mswindows}{$apptype console}{$endif} // covers all windows including wince
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018