Skip to main content
Engineering LibreTexts

24.2: Conditional Compilation

  • Page ID
    10548
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Overview

    As you proceed in your programming career, the problems/tasks that need solving become more complex. The documentation of the algorithm done in pseudo code (or some other method) will still need to be converted into a programming solution. Inevitably, when writing that source code mistakes will be introduced. When learning the syntax of a new programming language, programmers sometimes automatically think in their old language syntax, and make mistakes that are sometimes hard to detect.

    The concept of using a flag to either activate or have remain dormant certain lines of code designed solely to help with the debugging of a program has existed since almost the beginning of modern computer programming (1950's). One of the debugging tools available within C++ is conditional compilation. For our flag, we would use a defined constant like:

    #define DEBUG 1

    Then using another compiler directive pair, the #if and #endif, we can have the compiler during the pre-processor either include or not include one or more lines of code.

    #if DEBUG
      cout << "\n***** DEBUG Code ** Hi mom!";
    #endif
    

    Of course saying "Hi mom!" is not very useful for debugging your code. However, you can use test data with conditional compilation. A series of input data values and a series of output predictors can be placed in the program. Then you can turn on the debug feature or turn them off with your debugging flag.

    You should study the demonstration program in conjunction with this module.

    Demonstration Program in C++

    Creating a Folder or Sub-Folder for Source Code Files

    Depending on your compiler/IDE, you should decide where to download and store source code files for processing. Prudence dictates that you create these folders as needed prior to downloading source code files. A suggested sub-folder for the Bloodshed Dev-C++ 5 compiler/IDE might be named:

    • Demo_Programs

    If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.

    Download the Demo Program

    Download and store the following file(s) to your storage device in the appropriate folder(s). Following the methods of your compiler/IDE, compile and run the program(s). Study the source code file(s) in conjunction with other learning materials. You may need to right click on the link and select "Save Target As" in order to download the file.

    Download from Connexions: Demo_Conditional_Compliation.cpp

    Definitions

    Conditional Compilation
    A compiler directive that includes or excludes lines of code based on a Boolean expression

    This page titled 24.2: Conditional Compilation is shared under a CC BY license and was authored, remixed, and/or curated by Kenneth Leroy Busbee (OpenStax CNX) .

    • Was this article helpful?