[fpc-pascal] Conditional compilation using {$IFOPT ...} and {$IF OPTION(...)}
Michael Van Canneyt
michael at freepascal.org
Thu Aug 26 08:58:19 CEST 2021
On Thu, 26 Aug 2021, LacaK via fpc-pascal wrote:
> Hello,
> looking at
> https://www.freepascal.org/docs-html/prog/progsu126.html#x139-1400002.4.1
>
> There is mentioned "OPTION(opt)" with comment: "evaluates to TRUE if a
> compiler option is set (mode MacPas only). It is equivalent to the {$IFOPT }
> directive."
>
> 1. What is equivalent usage for {$IFOPT R+}?
> {$IF OPTION(R)} ?
Yes.
>
> 2. What means comment "(mode MacPas only)"?
That you can use it only in macpas mode. In other modes you must use {$IFOPT}
> It will does not work for example for ObjPas mode? In my case Compilation
> fails ... (with single {$IF OPTION(R)} ) -> Error: Compile time expression:
> Wanted Boolean but got AnsiString at IF or ELSEIF
It only works for mode macpas.
> Can this OPTION be enabled for all modes?
Technically yes. Whether that's a good idea, I don't know.
>
> 3. Is there way how to do OR with {$IFOPT}?
> if I use {$IF OPTION(R) OR OPTION(D)} I get: "Error: Compile time
> expression: Wanted Boolean but got AnsiString at IF or ELSEIF" ...
> So is there way how to do it with {$IFOPT R+D+}?
No.
You need to do
{$IFOPT D+}
{$DEFINE DOMYTHING
{$ENDIF}
{$IFOPT R+}
{$DEFINE DOMYTHING
{$ENDIF}
{$IFDEF DOMYTHING}
...
{$ENDIF}
Which I think is the wiser choice in each case, since it avoids repetition
and allows you to give a more meaningful name to the 'option'...
Michael.
More information about the fpc-pascal
mailing list