Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Conditional compilation

From Wikipedia, the free encyclopedia
When a compiler produces a program which can change based on given parameters

Incomputer programming,conditional compilation is acompilation technique which results in differingexecutable programs depending onparameters specified. This technique is commonly used when these differences in the program are needed to run it ondifferent platforms, or with different versions of requiredlibraries orhardware.

Manyprogramming languages support conditional compilation. Typicallycompiler directives define or "undefine" certainvariables; 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 *)ifmac!=Nullthen(* macOS specific code *)elseifpc!=Null(* Windows specific code *)

InC and some languages with a similar syntax, this is done using an'#ifdef' directive.

A similar procedure, using the name "conditional comment", is used byMicrosoft Internet Explorer from version 5 to 9 to interpretHTML code. There is also a similar proprietary mechanism for adding conditional comments withinJScript, known as conditional compilation.[1]

Examples

[edit]

C# provides preprocessor directives for conditional compilation.[2]

#if DEBUGConsole.WriteLine("Debug version");#endif

Rust supports conditional compilation.[3]

#[cfg_attr(target_os ="linux", path ="linux.rs")]#[cfg_attr(windows, path ="windows.rs")]modos;

Criticism

[edit]

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.[4][5][6] When conditional compilation is done via apreprocessor that does not guarantee syntactically correct output in the source language, such as theC preprocessor, this may lead to hard-to-debug compilation errors,[7][8][9] which is sometimes called "#ifdef hell."[10][11]

References

[edit]
  1. ^"Conditional Compilation". Microsoft Corporation. Archived fromthe original on 2008-09-06. Retrieved2011-11-27.
  2. ^"Preprocessor directives - C# reference".learn.microsoft.com. Retrieved31 May 2025.
  3. ^"Conditional compilation - The Rust Reference".doc.rust-lang.org. Retrieved31 May 2025.
  4. ^Gazzillo, Paul; Wei, Shiyi (2019-05-27)."Conditional Compilation is Dead, Long Live Conditional Compilation!"(PDF).ICSE-NIER '19: Proceedings of the 41st International Conference on Software Engineering: New Ideas and Emerging Results.2019 IEEE/ACM 41st International Conference on Software Engineering: New Ideas and Emerging Results (ICSE-NIER). Montreal, QC, Canada: IEEE Press. pp. 105–108.doi:10.1109/ICSE-NIER.2019.00035.ISBN 978-1-7281-1758-4.Archived(PDF) from the original on 2022-11-07. Retrieved2023-01-21.
  5. ^Meinicke, Jens; Thüm, Thomas; Schröter, Reimar; Benduhn, Fabian; Leich, Thomas; Saake, Gunter (2017). Meinicke, Jens; Thüm, Thomas; Schröter, Reimar; Benduhn, Fabian (eds.).Quality Assurance for Conditional Compilation. Cham: Springer International Publishing. pp. 131–139.doi:10.1007/978-3-319-61443-4_12.ISBN 978-3-319-61443-4. Retrieved2023-01-21.{{cite book}}:|work= ignored (help)
  6. ^"compiler - How does conditional compilation impact product quality, security and code complexity?".Software Engineering Stack Exchange. Retrieved2023-01-21.
  7. ^Le, Duc; Walkingshaw, Eric; Erwig, Martin (2011-09-18). "#ifdef confirmed harmful: Promoting understandable software variation".2011 IEEE Symposium on Visual Languages and Human-Centric Computing (VL/HCC). pp. 143–150.doi:10.1109/VLHCC.2011.6070391.ISBN 978-1-4577-1246-3.
  8. ^"conditional compilation - Why should #ifdef be avoided in .c files?".Stack Overflow. Retrieved2023-01-21.
  9. ^"c++ - Dos and Don'ts of Conditional Compile".Stack Overflow. Retrieved2023-01-21.
  10. ^Preschern, Christopher (2019-07-03). "Patterns to escape the #ifdef hell".Proceedings of the 24th European Conference on Pattern Languages of Programs(PDF). New York, NY, USA: Association for Computing Machinery. pp. 1–12.doi:10.1145/3361149.3361151.ISBN 978-1-4503-6206-1.Archived(PDF) from the original on 2022-12-21.
  11. ^"Living in the #ifdef Hell".www.cqse.eu. 28 October 2015.Archived from the original on 2022-11-28. Retrieved2023-01-21.
Retrieved from "https://en.wikipedia.org/w/index.php?title=Conditional_compilation&oldid=1322572155"
Category:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp