Category: 

What Is Conditional Compilation?

Article Details
  • Written By: Eugene P.
  • Edited By: Angela B.
  • Last Modified Date: 05 September 2016
  • Copyright Protected:
    2003-2016
    Conjecture Corporation
  • Print this Article
Free Widgets for your Site/Blog
In late 19th-century London, mail was delivered to residential addresses up to twelve times each day.   more...

September 28 ,  1924 :  Two US military planes complete the first flights around the world.  more...

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.

Ad

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.

Ad

You might also Like

Recommended

Discuss this Article

Post your comments

exception 'Exception' with message 'error writing captcha: Duplicate entry '2147483647' for key 'PRIMARY'' in /ssd/www/wisegeek/public_html/_core/classes/public/Captcha.php:44
Stack trace:
#0 /ssd/www/wisegeek/public_html/_core/controls/public/ControlDiscussionPostBox.php(324): Captcha->createCaptcha()
#1 /ssd/www/wisegeek/public_html/framework/classes/Control.php(104): ControlDiscussionPostBox->preRender(false)
#2 /ssd/www/wisegeek/public_html/framework/classes/Control.php(149): Control->render()
#3 /ssd/www/wisegeek/public_html/tpl/default-nocustom-lu/pages/public/article/article.htm(526): Control->__toString()
#4 /ssd/www/wisegeek/public_html/framework/classes/Control.php(300): require('/ssd/www/wisege...')
#5 /ssd/www/wisegeek/public_html/framework/classes/Control.php(309): Control->requireTpl('pages/public/ar...', Object(PageArticleCom), true)
#6 /ssd/www/wisegeek/public_html/framework/classes/Control.php(131): Control->renderTpl('pages/public/ar...', Object(PageArticleCom))
#7 /ssd/www/wisegeek/public_html/framework/classes/FormDataControl.php(87): Control->renderTemplate()
#8 /ssd/www/wisegeek/public_html/framework/classes/Control.php(109): FormDataControl->renderTemplate()
#9 /ssd/www/wisegeek/public_html/framework/classes/ScriptPage.php(50): Control->render(false)
#10 /ssd/www/wisegeek/public_html/framework/classes/Control.php(149): ScriptPage->render()
#11 /ssd/www/wisegeek/public_html/framework/classes/Page.php(97): Control->__toString()
#12 /ssd/www/wisegeek/public_html/_core/classes/public/PublicFrontController.php(443): Page->processRequest()
#13 /ssd/www/wisegeek/public_html/_core/classes/public/PublicFrontController.php(7): PublicFrontController->renderPage()
#14 /ssd/www/wisegeek/public_html/index.php(11): PublicFrontController::run()
#15 {main}