Conditionally compiling C++ macros

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yasmin
    New Member
    • Aug 2007
    • 4

    Conditionally compiling C++ macros

    I am trying to conditionally compile macros in my c++ program.
    I want to simply be able to use the macro name without enclosing it in #ifdef-#endif each time. How can I enclose the macro definition itself in a #ifdef-#endif?

    Here's my example:

    Code:
    #define FAULT
        { \ 
              if (test.exists()) { \
                  return error( "Aborting" ); \
              } \
        } \
    In several places in my program I have the macro inserted as follows:

    Code:
    cout << "Some valid code";
    FAULT
    cout << "Some more valid code";
    I want to be able to conditionally compile-in the macro FAULT if the variable "TESTME" is defined (i.e. it is provided at compile-time). Conversely, if TESTME is not defined then all instances of FAULT should be compiled out.

    Most importantly- I want to be able to do this without changing all instances of FAULT as follows:

    Code:
    cout << "Some valid code";
    #ifdef TESTME
    FAULT
    #endif
    cout << "Some more valid code";

    Here's what I already tried that didn't work:
    1) Enclosing the macro itself within #ifdef-#endif
    This causes the compiler to complain when it encounters instances of 'FAULT' in the code (since the macro definition no longer exists).
    Code:
    #ifdef TESTME
    #define FAULT
        { \ 
              if (test.exists()) { \
                  return error( "Aborting" ); \
              } \
        } \
    #endif
    2) I tried adding a #else clause to the macro defintion as follows. This did nothing.
    Code:
    #ifdef TESTME
    #define FAULT
        { \ 
              if (test.exists()) { \
                  return error( "Aborting" ); \
              } \
        } \
    #else
    #endif
    3) I also tried enclosing the contents of the macro within a #ifdef-#endif as follows. This didn't compile either.

    Code:
    #define FAULT
        { \ 
    #ifdef TESTME \
              if (test.exists()) { \
                  return error( "Aborting" ); \
              } \
    #endif \
        } \

    I'll really appreciate any other ideas!
    Last edited by yasmin; Aug 8 '07, 05:02 PM. Reason: Improving title
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The onhe thing you didn't try was:

    [code=cpp]
    #ifdef TESTME
    #define FAULT
    { \
    if (test.exists()) { \
    return error( "Aborting" ); \
    } \
    } \
    #else
    #define FAULT
    #endif
    [/code]

    So if you are not testing FAULT is defined but has no defined value. So nothing is expanded.

    Comment

    • Matthew Page
      New Member
      • Jul 2007
      • 36

      #3
      You might try adding another definition for FAULT in the else clause... That way in either case you get a definition for FAULT.

      Comment

      • yasmin
        New Member
        • Aug 2007
        • 4

        #4
        Thanks, that really helped!

        Originally posted by weaknessforcats
        The onhe thing you didn't try was:

        [code=cpp]
        #ifdef TESTME
        #define FAULT
        { \
        if (test.exists()) { \
        return error( "Aborting" ); \
        } \
        } \
        #else
        #define FAULT
        #endif
        [/code]

        So if you are not testing FAULT is defined but has no defined value. So nothing is expanded.

        Comment

        Working...