2.2 Macros

Macros are very much like symbols or compile-time variables in their syntax, the dierence is that macros have a value whereas a symbol simply is dened or is not dened. Furthermore, following the denition of a macro, any occurrence of the macro in the pascal source will be replaced with the value of the macro (much like the macro support in the C preprocessor). If macro support is required, the -Sm command-line switch must be used to switch it on, or the directive must be inserted:
{$MACRO ON}

otherwise macros will be regarded as a symbol.

Dening a macro in a program is done in the same way as dening a symbol; in a f$defineg preprocessor statement1 :

{$define ident:=expr}

If the compiler encounters ident in the rest of the source le, it will be replaced immediately by expr. This replacement works recursive, meaning that when the compiler expanded one macro, it will look at the resulting expression again to see if another replacement can be made. This means that care should be taken when using macros, because an innite loop can occur in this manner.

Here are two examples which illustrate the use of macros:

{$define sum:=a:=a+b;}  
...  
sum          { will be expanded to 'a:=a+b;'  
               remark the absence of the semicolon}  
...  
{$define b:=100}  
sum          { Will be expanded recursively to a:=a+100; }  
...

The previous example could go wrong:

{$define sum:=a:=a+b;}  
...  
sum          { will be expanded to 'a:=a+b;'  
               remark the absence of the semicolon}  
...  
{$define b=sum} { DON'T do this !!!}  
sum          { Will be infinitely recursively expanded \dots }  
...

On my system, the last example results in a heap error, causing the compiler to exit with a run-time error 203.

Remark:Macros dened in the interface part of a unit are not available outside that unit! They can just be used as a notational convenience, or in conditional compiles.

By default the compiler predenes three macros, containing the version number, the release number and the patch number. They are listed in table (2.1).


Table 2.1: Predened macros



Symbol Contains


FPC_VERSIONThe version number of the compiler.
FPC_RELEASEThe release number of the compiler.
FPC_PATCH The patch number of the compiler.



Remark:Don't forget that macro support isn't on by default. It must be turned on with the -Sm command-line switch or using the f$MACRO ONg directive.