Learn something new every day
More Info... by email
In computer programming, conditional compilation describes the ability of a software compiler or source code processor to include or ignore certain commands or blocks of code based on language-specific instructions that are not technically part of the core programming language specification. The commands that are used to trigger conditional compilation in a program often are called pre-processor directives, although they also can be known as compiler directives, conditional comments or conditional defines. Depending on the compiler or language being used, the conditional directives can be user-defined variables or macros, or they also could be system variables set by the compiler or the operating system. Conditional compilation frequently is used to configure a single set of source code files so they can be compiled under different environments or operating systems without having to swap or change source files.
The conditional defines that are used to test for conditional compilation can vary from one language to another, but they generally are macros or other meta-data expressions outside the scope of the normal program variables. In languages such as C, the variables usually are macros that are read by a pre-processor. Most of the time, the macros are not accessible by actual executable program source code, even though they exist in the same source files.
The directives that are used to test the defined conditional variables are rudimentary and follow the same logic as an if-then statement. The basic tests that can be performed for conditional compilation are whether a value is defined, not defined or, in some cases, whether two defined variables are equivalent. The variables or macros themselves generally do not hold any value of significance, because the act of defining them is what the directives are checking for. With some languages and compilers, however, the value of a defined variable can matter if they are evaluated for the Boolean state.
When a compiler encounters directives used for conditional compilation, the directives are evaluated to see whether they test as true or false. If true, the code that is following the directive is compiled as normal; otherwise, the compiler will completely skip the code contained under the directive. This is unlike conditional logic statements within the executable program code itself because, even if the logic has a block of code that cannot be executed, it still will be interpreted and compiled in most cases. With conditional pre-processor directives, skipped-over code is never included, interpreted or compiled. It also should be noted that once the pre-processor has evaluated all the conditional variables and directives, they are not compiled with the rest of the program, because they are intended only to provide direction to the compiler.
Conditional compilation most often is used to include or exclude code that is specific to a particular operating system or environment. This means libraries that exist in one operating system can be conditionally included if the directives determine the program is being compiled under that operating system. Alternately, the directives also can be used to include or exclude code based on user-defined variables. This can be done so debugging or profiling code is not included in the final version of a program, or so certain features can be enabled or disabled without having to maintain separate copies of the code.
One of our editors will review your suggestion and make changes if warranted. Note that depending on the number of suggestions we receive, this can take anywhere from a few hours to a few days. Thank you for helping to improve wiseGEEK!