In
computer programming
Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, 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 language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s support conditional compilation. Typically
compiler directives
In computer programming, a directive or pragma (from "pragmatic") is a language construct that specifies how a compiler (or other Translator (computing), translator) should process its input. Depending on the programming language, directives may o ...
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
Internet Explorer (formerly Microsoft Internet Explorer and Windows Internet Explorer, commonly abbreviated as IE or MSIE) is a retired series of graphical web browsers developed by Microsoft that were used in the Windows line of operating ...
from version 5 to 9 to interpret
HTML
Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets ( ...
code. There is also a similar proprietary mechanism for adding conditional comments within
JScript, known as conditional compilation.
C# have preprocessor directives for conditional compilation.
#if DEBUG
Console.WriteLine("Debug version");
#endif
Rust have conditional compilation.
# fg_attr(target_os = "linux", path = "linux.rs")# fg_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.
When conditional compilation is done via a
preprocessor
In computer science, a preprocessor (or precompiler) is a Computer program, program that processes its input data to produce output that is used as input in another program. The output is said to be a preprocessed form of the input data, which i ...
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."
[{{Cite web , title=Living in the #ifdef Hell , url=http://www.cqse.eu/en/news/blog/living-in-the-ifdef-hell/ , access-date=2023-01-21 , website=www.cqse.eu , date=28 October 2015 , language=en-GB, archive-url=https://web.archive.org/web/20221128024445/https://www.cqse.eu/en/news/blog/living-in-the-ifdef-hell/ , archive-date=2022-11-28 , url-status=live]
References
Programming language implementation