#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    9
    Rep Power
    0

    Conditional compilation...


    what are the real advantages between these two code snipplets below besides the obvious use of an unsigned char variable:

    Code:
    #define _DEBUG_
    #ifdef _DEBUG_
     // do something here
    #else
     // do something else here
    #endif
    Code:
    unsigned char i;
    i = 1;
    if(i)
     // do something here
    else
     // do something else here
    endif
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    The first method only generates code for one of the conditions. The second method generates code for both conditions.

    Jim
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    In the second example, you decide at run-time which path to take. In fact, you could revisit that same decision several times (or even millions of times) and take different paths through that code each time depending on the condition that you're testing.

    In conditional compilation, you are deciding whether or not to include that code in the source. This is performed by the pre-processor which goes through the original source code #include'ing header files and expanding macros. And including or excluding blocks of code based on conditional compilation. The result is a new source file that is then compiled.

    For example, we can have a baseline for a standard product. Then a new product that is based on that baseline and still uses most of the original code but with several modifications can still use the same files, but conditional compilation is used either to include those many modifications if we're building it for the new product or to exclude those modifications if we're building it for the original standard product.

    Another example is that when I'm fixing bugs I make use of conditional compilation. I'll #define a macro like BUG_456. Then when I find the places in the code which are causing the bug, then I'll use that macro; eg:
    Code:
    void foobar()
    {
        int i, j, k;
        char s[42];
    
    #ifdef BUG_456
        // code with the bug fixes
    #else  BUG_456
        // original buggy code
    #endif BUG_456
        printf("blah, blah\n");
    }
    At any time, I can back out those fixes (eg, they are not complete or tested yet and we have to do a build for a customer or management decides that that change is a lower priority and shouldn't be done yet) by #undef'ing BUG_456. And in reporting what actions I had taken in fixing that bug or porting it to another product that's using a different baseline, I can find all changes by searching for BUG_456. BTW, the #else and #endif do not need a macro; I just add them essentially as comments for self-documentation purposes.

    Take a look in the header files of your compiler's INCLUDE directory. You should see several instances of conditional compilation there that depend on the type of system or on the compiler version.

IMN logo majestic logo threadwatch logo seochat tools logo