[Lazarus] Conditional compilation depending on Lazarus version?
Sven Barth
pascaldragon at googlemail.com
Sat Mar 24 20:00:54 CET 2012
On 24.03.2012 19:35, Mark Morgan Lloyd wrote:
> Sven Barth wrote:
>> Am 22.03.2012 14:10, schrieb Mark Morgan Lloyd:
>>> Sven Barth wrote:
>>>> Am 22.03.2012 13:28, schrieb Mark Morgan Lloyd:
>>>>> Sven Barth wrote:
>>>>>> Am 22.03.2012 11:32, schrieb Reinier Olislagers:
>>>>>
>>>>>>> I realize that the code above will be compiled by FPC, not Lazarus;
>>>>>>> perhaps it is possible to set an environment variable
>>>>>>> LAZARUSREVISION
>>>>>>> and/or LAZARUSVERSION and get these into FPC macros in some way?
>>>>>>
>>>>>> The revision is not possible (when using development versions you
>>>>>> should try to use the latest revision), but for version you can use
>>>>>> the unit LCLVersion. Just add it to the uses clause and then you can
>>>>>> use e.g.:
>>>>>>
>>>>>> {$if lcl_fullversion>=93100}
>>>>>> // code that should work with 0.9.31 or newer
>>>>>> {$endif}
>>>>>
>>>>> For the record, can you easily say what version of FPC introduced this
>>>>> capability?
>>>>>
>>>>
>>>> According to a svn blame for the corresponding code this is in there
>>>> at least since 2005 (when FPC switched from CVS to SVN and thus no
>>>> older history is available to the public)
>>>
>>> Which probably makes it safe for 2.2.4, which corresponds approximately
>>> to 0.9.24.1 by which time the new-format lcl_fullversion etc. were
>>> available.
>>
>> These constants were introduced EXACTLY for the purpose to use them in
>> ifdefs. So yes, they are safe to use in this way ;)
>
> I've been having a bit of a play with this. Using the example above, am
> I correct in saying that this works
>
> {$if lcl_fullversion>=93100}
> // code that should work with 0.9.31 or newer
> {$endif}
>
> but this doesn't
>
> {$ifdef lcl_fullversion }
> // code that should work with any version that defines lcl_fullversion
> {$endif}
>
> As a variation of the latter, am I correct in saying that ifdef can't be
> used to check whether a function or procedure is implemented in some
> imported unit?
>
$ifdef does only work with defines (those done with -dXXX or {$define
...}), but not with identifiers/symbols (lcl_fullversion is a symbol).
If you want to check for the existance of a symbol you need to use this:
{$if declared(lcl_fullversion) }
// code that should work with any version that defines lcl_fullversion
{$endif}
// Note: if you need Delphi compatibility you need to use {$ifend}
instead of {$endif} here
Regards,
Sven
More information about the Lazarus
mailing list