Conditionally compile units for FMX or VCL

I want to have a different list of units in the uses depending on the compilation for FMX or VCL. In the code below, I'm trying to test FireMonkeyVersion , which works in the FMX project ( label1.Text is "FMX"). When I transfer the $ IF statement to the uses , I get an error message ( [dcc32 Error] fmx_text.pas(7): E2026 Constant expression expected ). Is there a way to get the desired conditional compilation?

 unit fmx_text; interface uses System.SysUtils, System.Types, System.UITypes, System.Rtti, System.Classes, System.Variants, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.Types; {$IF FireMonkeyVersion >= 16} {$DEFINE HAS_FMX} {$ELSE} {$DEFINE HAS_VCL} {$IFEND} type TForm2 = class(TForm) Label1: TLabel; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form2: TForm2; implementation {$R *.fmx} procedure TForm2.FormCreate(Sender: TObject); begin label1.Text := 'Undefined'; {$IFDEF HAS_FMX} label1.Text := 'FMX'; {$ENDIF} {$IFDEF HAS_VCL} label1.Text := 'VCL'; {$ENDIF} end; end. 
+4
source share
1 answer

FireMoneyVersion not a value defined by the compiler. This is a named constant declared in the FMX.Types module. Try using {$IF DECLARED(FireMonkeyVersion)} or {$IF DEFINED(FireMonkeyVersion)} (I forgot which one supports Delphi), for example:

 {$DEFINE HAS_VCL} {$IF DECLARED(FireMonkeyVersion)} {$IF FireMonkeyVersion >= 16} {$UNDEF HAS_VCL} {$DEFINE HAS_FMX} {$IFEND} {$ENDIF} 

If this works, I do not see reaon to check its value. You either have FireMonkey or not:

 {$IF DECLARED(FireMonkeyVersion)} {$DEFINE HAS_FMX} {$ELSE} {$DEFINE HAS_VCL} {$IFEND} 

With that said, keep in mind that it is possible (although not officially supported) to mix FireMonkey and VCL together in the same project. Therefore, you may need to rethink everything that you are trying to accomplish by differentiating the framework.

+6
source

Source: https://habr.com/ru/post/1435465/


All Articles