Sign in Join
FPDoc Onlineexperiment

Conditionals

The rules for using conditional symbols are the same as under Turbo Pascal or Delphi. 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 ...} 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 code, 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.

Predefined symbols

The Free Pascalcompiler defines some symbols before starting to compile your program or unit. You can use these symbols to differentiate between different versions of the compiler, and between different compilers. To get all the possible defines when starting compilation, see appendix Compiler defines during compilation

Remark: Symbols, even when they’re defined in the interface part of a unit, are not available outside that unit.

An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.