Recent

Author Topic: Compiler definiton for lcl like {$IF LCL_VERSION>2} ?  (Read 4266 times)

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: Compiler definiton for lcl like {$IF LCL_VERSION>2} ?
« Reply #15 on: May 17, 2019, 04:49:07 pm »
I have now thoroughly checked this issue again with Lazarus versions 1.2.6, 1.6.2, 1.8.4 and 2.0.2 on Linux, macOS and Windows.

If I use the LCL_FullVersion conditional (or some of its equivalents like LCL_MAJOR) in the implementation part everything works well. However, using it in the interface part raises the error as described. See a short example in the next section or in the attached file with a demo project. This project can only be compiled after commenting out lines 10 to 12 of unit1.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   ExtCtrls, LCLVersion
  10.   {$IF LCL_MAJOR >= 2}           // <- This doesn't work
  11.     , LazVersion
  12.   {$ENDIF}
  13.   ;
  14.  
  15. ...
  16.  
  17. implementation
  18.  
  19. ...
  20.  
  21. procedure TForm1.FormCreate(Sender: TObject);
  22. begin
  23.   {$IF LCL_MAJOR >= 2}              // <- This works
  24.     RadioGroup1.ItemIndex := 2;
  25.   {$ELSE}
  26.     {$IF LCL_MAJOR >= 1}
  27.       RadioGroup1.ItemIndex := 1;
  28.     {$ELSE}
  29.       RadioGroup1.ItemIndex := 0;
  30.     {$ENDIF}
  31.   {$ENDIF}
  32. end;
  33.  
  34. end.

This is a pity, since conditionals are much more needed at compile time than at runtime.
« Last Edit: May 17, 2019, 04:50:47 pm by jwdietrich »
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

Zoran

  • Hero Member
  • *****
  • Posts: 1831
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Compiler definiton for lcl like {$IF LCL_VERSION>2} ?
« Reply #16 on: May 17, 2019, 05:28:55 pm »
Ah, I see now what you are trying to do.
You are trying to use the constant before uses section ended.
No, you can't do that.

However, you can use it in interface section, but parser needs to finish the uses block before it can see the constants.

This compiles well:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs,
  9.   LCLVersion
  10.   ;
  11.  
  12. {$if lcl_major >= 2}
  13. type
  14.   TSomething = Integer;
  15. {$endif}
  16.  
  17. type
  18.   TForm1 = class(TForm)
  19.   private
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. end.
  33.  
« Last Edit: May 17, 2019, 05:40:05 pm by Zoran »

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Compiler definiton for lcl like {$IF LCL_VERSION>2} ?
« Reply #17 on: May 17, 2019, 06:57:28 pm »
Or, if possible, put the conditionally excluded unit into the uses clause of the implementation section. This works for me:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   ExtCtrls, LCLVersion;
  10.  
  11. ...
  12.  
  13. implementation
  14.  
  15. {$IF LCL_MAJOR >= 2}
  16. uses
  17.   LazVersion;
  18. {$ENDIF}
  19.  
  20. ...
  21.  
« Last Edit: May 17, 2019, 07:24:53 pm by wp »

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: Compiler definiton for lcl like {$IF LCL_VERSION>2} ?
« Reply #18 on: May 17, 2019, 11:21:29 pm »
Thanks, @Zoran and @wp. It works now. At least for me, a major issue that thwarted the evolution of one of my major projects is solved.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

 

TinyPortal © 2005-2018