Conditional compilation explained
In computer programming, conditional compilation is a compilation technique which results in differring 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:
(* 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.[1]
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.[2] [3] [4] 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,[5] [6] [7] which is sometimes called "#ifdef hell."[8] [9]
References
- Web site: Conditional Compilation. Microsoft Corporation. 2011-11-27. https://web.archive.org/web/20080906144358/http://msdn2.microsoft.com/en-us/library/ahx1z4fs(VS.80).aspx. 2008-09-06. dead.
- Conditional Compilation is Dead, Long Live Conditional Compilation!. Gazzillo. Paul. Wei. Shiyi. 2019-05-27. 2019 IEEE/ACM 41st International Conference on Software Engineering: New Ideas and Emerging Results (ICSE-NIER). https://2019.icse-conferences.org/. ICSE-NIER '19: Proceedings of the 41st International Conference on Software Engineering: New Ideas and Emerging Results. IEEE Press. https://web.archive.org/web/20221107174330/https://www.paulgazzillo.com/papers/icse19nier.pdf. 2022-11-07. live. Montreal, QC, Canada. 105–108. 978-1-7281-1758-4. 10.1109/ICSE-NIER.2019.00035. 2023-01-21. en.
- Book: Meinicke . Jens . Quality Assurance for Conditional Compilation . 2017 . Mastering Software Variability with FeatureIDE . 131–139 . Meinicke . Jens . Cham . Springer International Publishing . en . 10.1007/978-3-319-61443-4_12 . 978-3-319-61443-4 . 2023-01-21 . Thüm . Thomas . Schröter . Reimar . Benduhn . Fabian . Leich . Thomas . Saake . Gunter . Thüm . Thomas . Schröter . Reimar . Benduhn . Fabian.
- Web site: compiler - How does conditional compilation impact product quality, security and code complexity? . 2023-01-21 . Software Engineering Stack Exchange . en.
- Book: Le . Duc . Walkingshaw . Eric . Erwig . Martin . 2011 IEEE Symposium on Visual Languages and Human-Centric Computing (VL/HCC) .
- ifdef confirmed harmful: Promoting understandable software variation
. 2011-09-18 . 143–150 . 10.1109/VLHCC.2011.6070391. 978-1-4577-1246-3 .
- Web site: conditional compilation - Why should #ifdef be avoided in .c files? . 2023-01-21 . Stack Overflow . en.
- Web site: c++ - Dos and Don'ts of Conditional Compile . 2023-01-21 . Stack Overflow . en.
- Preschern . Christopher . 2019-07-03 . Patterns to escape the #ifdef hell . Proceedings of the 24th European Conference on Pattern Languages of Programs . EuroPLop '19 . New York, NY, USA . Association for Computing Machinery . 1–12 . 10.1145/3361149.3361151 . 978-1-4503-6206-1 . https://web.archive.org/web/20221221151021/http://preschern.azurewebsites.net/2019-os-specific-code.pdf. 2022-12-21. live.
- Web site: Living in the #ifdef Hell . 2023-01-21 . www.cqse.eu . 28 October 2015 . en-GB. https://web.archive.org/web/20221128024445/https://www.cqse.eu/en/news/blog/living-in-the-ifdef-hell/ . 2022-11-28 . live.