[next] [tail] [up]

### 2.1 Conditionals

The rules for using conditional symbols are the same as under Turbo Pascal. Defining a symbol goes as follows:

     {$define Symbol}

From this point on in your code, the compiler knows the symbol Symbol. Symbols are, like the Pascal language, case insensitive.

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}

If the symbol didn’t exist yet, this doesn’t do anything. If the symbol existed previously, the symbol will be erased, and will not be recognized any more in the code following the {$undef \dots} statement.

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}

In this example, if MySymbol exists, then the call to DoSomething will be compiled. If it doesn’t exist, the call to DoSomethingElse is compiled.

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}

In this directive expr is a Pascal expression which is evaluated using strings, unless both parts of a comparision can be evaluated as numbers, in which case they are evaluated using numbers1. If the complete expression evaluates to '0', then it is considered false and rejected. Otherwise it is considered true and accepted. This may have unexpected consequences:

     {$if 0}

will evaluate to False and be rejected, while

     {$if 00}

will evaluate to True.

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.

As you can see from the example, this construct isn’t useful when used with normal symbols, only if you use macros, which are explained in section 2.3, page 166. They can be very useful. When trying this example, you must switch on macro support, with the -Sm command-line switch.
Predefined symbols

[next] [front] [up]