Conditional compilation

From Free Pascal wiki
Jump to: navigation, search

Deutsch (de) English (en) français (fr)

What is conditional compilation?

Conditional compilation is compiling or omitting part of the sourcecode based on if a condition exists or not.
The features that make this possible in most compiled languages are called compile time directives. Compile time directives make it possible to compile a block of code based on the presence or absense of a condition at compile time. They can be used for a variety of purposes like:

  • Platform specific code isolation
  • Natural language selection
  • Licensing opensource and closed source parts
  • Isolating experimental code
  • Compiler version
  • Library version
  • etc, etc.

FreePascal supports four different styles of conditional compilation:

  • TurboPascal and early Delphi style directives
  • Mac Pascal style directives
  • Modern Freepascal and Delphi style directives
  • Compile time Macro's

Note the syntax here is not case sensitive as conforms to all Pascal syntax. We will use both lowercase and uppercase examples. We will show you the difference between the modes and how to efficiently use them.

TurboPascal style directives

The TurboPascal style directives are {$DEFINE}, {$IFDEF}, {$ENDIF}, {$IFNDEF}, {$IFOPT}, {$ELSE}, {$ELSEIF} and {$UNDEF}.
We will describe the directives in the context of the style. Some defines have an extended meaning in another style.
That means later on we may expand the meaning of certain directives like e.g. {$DEFINE} in the context of Macro's.


The {$DEFINE} directive simply declares a symbol that we later can use for conditional compilation:

{$DEFINE name} // This defines a symbol called "name"
 Note you can also define a symbol from the command line or the IDE -dDEBUG for example is the command line equivalent of {$DEFINE DEBUG} in the sourcecode.

The {$UNDEF} directive undefines a previously defined symbol. Here is an example that the author uses in practice:

// Some older sourcecode is polluted with {$IFDEF FPC}'s that are no 
// longer necessary depending on the Delphi version to which it it should be compatible.
// I always test this by trying this on top of the program or unit:
  {$DEFINE VER150} 
  // code will now compile as if it was Delphi 7, provided the original Delphi sourcecode was indeed written for Delphi 7 and up.
$ifdef and $endif

The simplest way to define a block of conditional code is like this:

unit cross;

The above example is quite common for sourcecode that has to compile in both Delphi and Freepascal</br> If the compiler is Delphi then nothing is done, but if the compiler is FreePascal it will switch Freepascal to compile and use Delphi syntax mode.
This "FPC" conditional symbol is defined in system - there is a long list of those. The {$IFDEF} and {$ENDIF} block syntax is symmetrical: every {$IFDEF} has its own {$ENDIF}.
To help you recognize the corresponding blocks you can use e.g. indentation, but you can also use the comment feature:

{$IFDEF FPC this part is Freepascal specific}
// some Freepascal specific code
{$ENDIF Freepascal specific code}
This comment feature is often not well understood. Some people - as on an older version of this wiki entry - assumed you could nest {$IFDEF}'s because the compiler seems to accept the syntax. But the former is false and the latter is true: Yes the compiler accepts the syntax below, but it is not a nested {$IFDEF} but a single {$IFDEF} condition and the rest is a comment!" The code below executes the writeln if and only if red is defined.
In this example {$ifdef blue} is a comment! Even if the {$define blue} is valid.
program completelywrong;
{$define blue}  
{$ifdef red or $ifdef blue}// everything after red is a comment
  writeln ('red or blue'); // this code is never reached
{$endif red or blue}       // everything after $endif is a comment.

This is the opposite of {$IFDEF} and code will be included of a certain condition is not defined.

A simple example is:
{$IFNDEF FPC this part not for Freepascal}
// some specific code that Freepascal should not compile
{$ENDIF code for other compilers than Freepascal}
$else and $elseif

{$ELSE} is used to compile code that does not belong to the codeblock that is defined by the corresponding {$IFDEF}. It is also valid in the context {$IFOPT}, {$IF} or {$IFC} that we will discuss later.

{$IFDEF red}  
   writeln('Red is defined');  
{$ELSE  no red}  
  {$IFDEF blue}  
   writeln('Blue is defined, but red is not defined');  
  {$ELSE no blue}  
  writeln('Neither red nor blue is defined'); 
  {$ENDIF blue}  
{$ENDIF red}

Such nested conditional written in the above syntax can get very messy and unreadable. Luckily we can simplify it a lot by using {$ELSEIF}. The code below is an expanded equivalent of the first example:

{$IFDEF red}  
  writeln('Red is defined');  
{$ELSEIF blue}  
  writeln('Blue is defined');  
{$ELSEIF green}  
  writeln('Green is defined');   
  writeln('Neither red, blue or green. Must be black...or something else...');

As you can see this is a lot more readable.


With {$IFOPT} we can check if a certain compile option is defined. From the programmers manual:

The {$IFOPT switch} will compile the text that follows it if the switch switch is currently in the specified state. If it isn’t in the specified state, then compilation continues after the corresponding {$ELSE} 
or {$ENDIF} directive.
As an example:
 {$IFOPT M+}  
   Writeln(’Compiled with type information’);  
Will compile the Writeln statement only if generation of type information is on.
Remark:The {$IFOPT} directive accepts only short options, i.e. {$IFOPT TYPEINFO} will not be accepted.
A common use is this example to test if DEBUG mode is defined:
{$IFOPT D+}{$NOTE debug mode is active}{$ENDIF}

Such defines can also reside in configuration files like fpc.cfg which also contains a full explanation on how to use:

# ----------------------
# Defines (preprocessor)
# ----------------------
# nested #IFNDEF, #IFDEF, #ENDIF, #ELSE, #DEFINE, #UNDEF are allowed
# -d is the same as #DEFINE
# -u is the same as #UNDEF
# Some examples (for switches see below, and the -? helppages)
# Try compiling with the -dRELEASE or -dDEBUG on the commandline
# For a release compile with optimizes and strip debuginfo
  #WRITE Compiling Release Version

Examples of bad use of conditional compilation: What not to do

What is wrong with this code? Can you spot it?
  {$ifdef Win32} 

The answer is:

  • that Freepascal compiles for more CPU types than 32 an 64 bit, also for e.g. 8 and 16 bit.
  • on most 64 bit platforms the maximum file size is a qword, not an int64.

That programmer fell into a trap that is common: if you use a define, make sure your logic is solid. Otherwise such code can easily cause accidents. The compiler will not catch your logic errors!
It is always good to realize such things especially that such things can easily be fixed.

  {$ifdef Win32} 
  {$elseif Win64}
     {$error this code is written for win32 or win64}

As an aside of course there is a solution for this particular example that does not use conditionals at all:


What is wrong with this code? Can you spot it?
  {$IFDEF BLUE AND $IFDEF RED} Form1.Color := clYellow; {$ENDIF}
  {$IFNDEF RED AND $IFNDEF BLUE} Form1.Color := clAqua; {$ENDIF}

The Answer is:

  • Well, I have already wrote a comment that warned you.. so look at the warning.... You should be able to spot it...
  • Compiler directives override the compiler... be careful with that axe Eugene.
Directives, Defines and Conditionals:
Conditional compilation | Conditional Compiler Options | global compiler directives | local compiler directives | $IF | Macros and Conditionals | Platform defines