有条件地编译FMX或VCL的单元 [英] Conditionally compile units for FMX or VCL

查看:598
本文介绍了有条件地编译FMX或VCL的单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 uses 子句中有不同的单元列表,具体取决于编译FMX或VCL。在下面的代码中,我尝试测试在FMX项目中工作的 FireMonkeyVersion label1.Text 是'FMX' 。当我移动$ IF语句到使用子句时,我得到一个错误信息( [dcc32 Error] fmx_text.pas(7):E2026常量表达式预期)。是否有任何方法来获得所需的条件编译?

I want to have a different list of units in the uses clause depending on compiling for FMX or VCL. In the code below I try to test FireMonkeyVersion which works in an FMX project (label1.Text is 'FMX'). When I move the $IF statement into the uses clause I get an error message ([dcc32 Error] fmx_text.pas(7): E2026 Constant expression expected). Is there any 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.


推荐答案

FireMoneyVersion 不是编译器定义的值。它是一个在 FMX.Types 单元中声明的命名常量。尝试使用 {$ IF DECLARED(FireMonkeyVersion)} {$ IF DEFINED(FireMonkeyVersion)} Delphi支持),例如:

FireMoneyVersion is not a compiler-defined value. It is a named constant declared in the FMX.Types unit instead. Try using {$IF DECLARED(FireMonkeyVersion)} or {$IF DEFINED(FireMonkeyVersion)} (I forget which one Delphi supports), eg:

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

如果有效,那么我没有看到一个reaon来检查它的值。您可以使用FireMonkey,也可以不使用:

If that works, then I don't see a reaon to check its value. You either have FireMonkey or you do not:

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


$ b b

有了这一点,请记住,有可能(虽然没有官方支持)在同一个项目中混合使用FireMonkey和VCL。所以你可能需要重新思考你想通过区分框架实现什么。

With that said, do keep in mind that it is possible (though not officially supported) to mix FireMonkey and VCL together in the same project. So you might need to re-think whatever you are trying to accomplish by differentiating the frameworks.

这篇关于有条件地编译FMX或VCL的单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆