Conditional compilation
When a compiler produces a program which can change based on given parameters
title: "Conditional compilation" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["programming-language-implementation", "articles-with-example-c-sharp-code", "articles-with-example-rust-code"] description: "When a compiler produces a program which can change based on given parameters" topic_path: "general/programming-language-implementation" source: "https://en.wikipedia.org/wiki/Conditional_compilation" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0
::summary When a compiler produces a program which can change based on given parameters ::
In computer programming, conditional compilation is a compilation technique which results in differing executable programs depending on parameters specified. This technique is commonly used when these differences in the program are needed to run it on different platforms, or with different versions of required libraries or hardware.
Many programming languages support conditional compilation. Typically compiler directives define or "undefine" certain variables; other directives test these variables and modify compilation accordingly. For example, not using an actual language, the compiler may be set to define "Macintosh" and undefine "PC", and the code may contain:
::code[lang=pascal] (* System generic code ) if mac != Null then ( macOS specific code ) else if pc != Null ( Windows specific code *) ::
In C and some languages with a similar syntax, this is done using an '#ifdef' directive.
A similar procedure, using the name "conditional comment", is used by Microsoft Internet Explorer from version 5 to 9 to interpret HTML code. There is also a similar proprietary mechanism for adding conditional comments within JScript, known as conditional compilation.{{cite web | url = http://msdn2.microsoft.com/en-us/library/ahx1z4fs(VS.80).aspx | title = Conditional Compilation | publisher = Microsoft Corporation | access-date = 2011-11-27 | archive-url = https://web.archive.org/web/20080906144358/http://msdn2.microsoft.com/en-us/library/ahx1z4fs(VS.80).aspx | archive-date = 2008-09-06 | url-status = dead
Examples
C# provides preprocessor directives for conditional compilation. ::code[lang=csharp] #if DEBUG Console.WriteLine("Debug version"); #endif ::
Rust supports conditional compilation. ::code[lang=rust] #[cfg_attr(target_os = "linux", path = "linux.rs")] #[cfg_attr(windows, path = "windows.rs")] mod os; ::
Criticism
When conditional compilation depends on too many variables, it can make the code harder to reason about as the number of possible combinations of configuration increases exponentially.{{cite conference | url= https://www.paulgazzillo.com/papers/icse19nier.pdf | title= Conditional Compilation is Dead, Long Live Conditional Compilation! | last1= Gazzillo | first1= Paul | last2= Wei | first2= Shiyi | date= 2019-05-27 | conference= 2019 IEEE/ACM 41st International Conference on Software Engineering: New Ideas and Emerging Results (ICSE-NIER) | conference-url= https://2019.icse-conferences.org/ | book-title= ICSE-NIER '19: Proceedings of the 41st International Conference on Software Engineering: New Ideas and Emerging Results | publisher= IEEE Press | archive-url= https://web.archive.org/web/20221107174330/https://www.paulgazzillo.com/papers/icse19nier.pdf | archive-date= 2022-11-07 | url-status= live | location= Montreal, QC, Canada | pages= 105–108 | isbn= 978-1-7281-1758-4 | doi= 10.1109/ICSE-NIER.2019.00035 | access-date= 2023-01-21 | language= en}} When conditional compilation is done via a preprocessor that does not guarantee syntactically correct output in the source language, such as the C preprocessor, this may lead to hard-to-debug compilation errors, which is sometimes called "#ifdef hell."
References
References
- "Preprocessor directives - C# reference".
- "Conditional compilation - The Rust Reference".
- (2017). "Quality Assurance for Conditional Compilation". Springer International Publishing.
- "compiler - How does conditional compilation impact product quality, security and code complexity?".
- (2011-09-18). "2011 IEEE Symposium on Visual Languages and Human-Centric Computing (VL/HCC)".
- "conditional compilation - Why should #ifdef be avoided in .c files?".
- "c++ - Dos and Don'ts of Conditional Compile".
- Preschern, Christopher. (2019-07-03). "Proceedings of the 24th European Conference on Pattern Languages of Programs". Association for Computing Machinery.
- (28 October 2015). "Living in the #ifdef Hell".
::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::