The rules for using conditional symbols are the same as under Turbo Pascal. Defining a symbol goes as follows:
{$define Symbol} |
You can also define a symbol on the command line. the -dSymbol option defines the symbol Symbol. You can specify as many symbols on the command line as you want.
Undefining an existing symbol is done in a similar way:
{$undef Symbol} |
You can also undefine symbols from the command line with the -u command-line switch.
To compile code conditionally, depending on whether a symbol is defined or not, you can enclose the code in a {$ifdef Symbol} . . . {$endif} pair. For instance the following code will never be compiled:
{$undef MySymbol} {$ifdef Mysymbol} DoSomething; ... {$endif} |
Similarly, you can enclose your code in a {$ifndef Symbol} . . . {$endif} pair. Then the code between the pair will only be compiled when the used symbol doesn’t exist. For example, in the following example, the call to the DoSomething will always be compiled:
{$undef MySymbol} {$ifndef Mysymbol} DoSomething; ... {$endif} |
You can combine the two alternatives in one structure, namely as follows
{$ifdef Mysymbol} DoSomething; {$else} DoSomethingElse {$endif} |
Except for the Turbo Pascal constructs the Free Pascal compiler also supports a stronger conditional compile mechanism: The {$if} construct.
The prototype of this construct is as follows:
{$if expr} CompileTheseLines; {$else} BetterCompileTheseLines; {$endif} |
{$if 0} |
{$if 00} |
You can use any Pascal operator to construct your expression: =, <>, >, <, >=, <=, AND, NOT, OR and you can use round brackets to change the precedence of the operators.
The following example shows you many of the possibilities:
{$ifdef fpc} var y : longint; {$else fpc} var z : longint; {$endif fpc} var x : longint; begin {$if (fpc_version=0) and (fpc_release>6) and (fpc_patch>4)} {$info At least this is version 0.9.5} {$else} {$fatal Problem with version check} {$endif} {$define x:=1234} {$if x=1234} {$info x=1234} {$else} {$fatal x should be 1234} {$endif} {$if 12asdf and 12asdf} {$info $if 12asdf and 12asdf is ok} {$else} {$fatal $if 12asdf and 12asdf rejected} {$endif} {$if 0 or 1} {$info $if 0 or 1 is ok} {$else} {$fatal $if 0 or 1 rejected} {$endif} {$if 0} {$fatal $if 0 accepted} {$else} {$info $if 0 is ok} {$endif} {$if 12=12} {$info $if 12=12 is ok} {$else} {$fatal $if 12=12 rejected} {$endif} {$if 12<>312} {$info $if 12<>312 is ok} {$else} {$fatal $if 12<>312 rejected} {$endif} {$if 12<=312} {$info $if 12<=312 is ok} {$else} {$fatal $if 12<=312 rejected} {$endif} {$if 12<312} {$info $if 12<312 is ok} {$else} {$fatal $if 12<312 rejected} {$endif} {$if a12=a12} {$info $if a12=a12 is ok} {$else} {$fatal $if a12=a12 rejected} {$endif} {$if a12<=z312} {$info $if a12<=z312 is ok} {$else} {$fatal $if a12<=z312 rejected} {$endif} {$if a12<z312} {$info $if a12<z312 is ok} {$else} {$fatal $if a12<z312 rejected} {$endif} {$if not(0)} {$info $if not(0) is OK} {$else} {$fatal $if not(0) rejected} {$endif} {$info *************************************************} {$info * Now have to follow at least 2 error messages: *} {$info *************************************************} {$if not(0} {$endif} {$if not(<} {$endif} end. |