Conditional compilation/ru

From Lazarus wiki
Revision as of 19:58, 8 December 2019 by Zoltanleo (talk | contribs) (Created page with "{{Conditional_compilation}} = What is conditional compilation? = Conditional compilation is compiling or omitting part of the source code based on if a condi...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Deutsch (de) English (en) suomi (fi) français (fr) русский (ru)

What is conditional compilation?

Conditional compilation is compiling or omitting part of the source code 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 absence of a condition at compile time. They are part of the compiler directive

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.

Free Pascal supports four different styles of conditional compilation:

  • Turbo Pascal and early Delphi style directives
  • Mac Pascal style directives
  • Modern Free Pascal and Delphi style directives
  • Compile time Macros

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.

Turbo Pascal style directives

The Turbo Pascal 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 Macros.


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


in the source code.


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

// Some older source code is polluted with {$IFDEF FPC} 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 source code 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 source code that has to compile in both Delphi and Free Pascal

If the compiler is Delphi then nothing is done, but if the compiler is Free Pascal it will switch Free Pascal 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 Free Pascal specific}
// some Free Pascal specific code
{$ENDIF Free Pascal 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} 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 completely wrong;
{$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 Free Pascal}
// some specific code that Free Pascal should not compile
{$ENDIF code for other compilers than Free Pascal}

$else and $elseif

{$ELSE} is used to compile code that does not belong to the code block 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:

{$IF Defined(red)}  
  writeln('Red is defined');  
{$ELSEIF Defined(blue)}  
  writeln('Blue is defined');  
{$ELSEIF Defined(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, ie {$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 -? help pages)
# Try compiling with the -dRELEASE or -dDEBUG on the command line
# For a release compile with optimizes and strip debug info
  #WRITE Compiling Release Version

What not to do

What is wrong with this code? Can you spot it?

  {$ifdef Win32} 

The answer is:

  • that Free Pascal 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.

  {$if defined(Win32)} 
  {$elseif defined(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, definitions and conditionals definitions
global compiler directives • local compiler directives

Conditional Compiler Options • Conditional compilation • Macros and Conditionals • Platform defines