Conditional compilation

Top  Previous  Next

Introduction

 

The conditional compilation directives allow sections of code to be selectively included for or excluded from compilation depending on whether the programmer-specified symbol (name) is defined or not. It is usually used as a portability tool for tailoring the program code to different hardware variants or to build multiple versions of an application with different functionality.

 

Another common use of conditional compilation is for temporarily omitting code. This is often done during testing and debugging when the programmer is experimenting with suspected areas of code. Although code may also be omitted by commenting its out, this approach does not work if the code already contains comments because such comments cannot be nested.

 

A conditional compilation name can either be defined with the #DEFINE directive or it can be defined in the Project Settings.

Both defining a name by using the #DEFINE directive or by doing it from the Project Settings have the same effect, but the latter is a more user-friendly approach that does not require modification or even access to the VPL source code itself (encrypted include file).

 

A conditional compilation name consists of up to 80 letters and numbers.

 

The following shows some examples of the usage of conditional compilation:

 

Example 1

#DEFINE DEBUG

 

#IFDEF DEBUG THEN

    ... this code will be included in the compilation.

#END_IF

 

 

Example 2

#DEFINE DEBUG

 

#IFDEF DEBUG THEN

    ... this code will be included in the compilation.

#ELSE

    ... this code will NOT be included in the compilation.

#END_IF

 

 

Example 3

#DEFINE DEBUG

 

#IFDEF NOT DEBUG THEN

    ... this code will NOT be included in the compilation.

#ELSE

    ... this code will be included in the compilation.

#END_IF

 

 

Example 4

#DEFINE BETA

 

#IFDEF BETA OR ALFA THEN

    ... this code will be included in the compilation.

#END_IF

 

#IFDEF ALFA THEN

    ... this code will not be included in the compilation.

#END_IF

 

#UNDEFINE BETA

 

#IFDEF BETA THEN

    ... this code will NOT be included in the compilation.

#END_IF

 

 

Example 5

#DEFINE BETA1

#DEFINE BETA2

 

#IFDEF BETA1 OR BETA3 THEN

    ... this code will be included in the compilation.

#END_IF

 

#IFDEF ALFA OR BETA1 THEN

    ... this code will be included in the compilation.

#END_IF

 

#IFDEF ALFA OR NOT BETA2 THEN

    ... this code will NOT be included in the compilation.

#END_IF