By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,530 Members | 880 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,530 IT Pros & Developers. It's quick & easy.

Conditionally compiling C++ macros

P: 4
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:

Expand|Select|Wrap|Line Numbers
  1. #define FAULT
  2.     { \ 
  3.           if (test.exists()) { \
  4.               return error( "Aborting" ); \
  5.           } \
  6.     } \
  7.  
  8.  
In several places in my program I have the macro inserted as follows:

Expand|Select|Wrap|Line Numbers
  1. cout << "Some valid code";
  2. FAULT
  3. 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:

Expand|Select|Wrap|Line Numbers
  1. cout << "Some valid code";
  2. #ifdef TESTME
  3. FAULT
  4. #endif
  5. 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).
Expand|Select|Wrap|Line Numbers
  1. #ifdef TESTME
  2. #define FAULT
  3.     { \ 
  4.           if (test.exists()) { \
  5.               return error( "Aborting" ); \
  6.           } \
  7.     } \
  8. #endif
2) I tried adding a #else clause to the macro defintion as follows. This did nothing.
Expand|Select|Wrap|Line Numbers
  1. #ifdef TESTME
  2. #define FAULT
  3.     { \ 
  4.           if (test.exists()) { \
  5.               return error( "Aborting" ); \
  6.           } \
  7.     } \
  8. #else
  9. #endif
3) I also tried enclosing the contents of the macro within a #ifdef-#endif as follows. This didn't compile either.

Expand|Select|Wrap|Line Numbers
  1. #define FAULT
  2.     { \ 
  3. #ifdef TESTME \
  4.           if (test.exists()) { \
  5.               return error( "Aborting" ); \
  6.           } \
  7. #endif \
  8.     } \

I'll really appreciate any other ideas!
Aug 8 '07 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
The onhe thing you didn't try was:

Expand|Select|Wrap|Line Numbers
  1. #ifdef TESTME
  2. #define FAULT
  3.     { \ 
  4.           if (test.exists()) { \
  5.               return error( "Aborting" ); \
  6.           } \
  7.     } \
  8. #else
  9.    #define FAULT
  10. #endif
  11.  
So if you are not testing FAULT is defined but has no defined value. So nothing is expanded.
Aug 8 '07 #2

P: 36
You might try adding another definition for FAULT in the else clause... That way in either case you get a definition for FAULT.
Aug 8 '07 #3

P: 4
Thanks, that really helped!

The onhe thing you didn't try was:

Expand|Select|Wrap|Line Numbers
  1. #ifdef TESTME
  2. #define FAULT
  3.     { \ 
  4.           if (test.exists()) { \
  5.               return error( "Aborting" ); \
  6.           } \
  7.     } \
  8. #else
  9.    #define FAULT
  10. #endif
  11.  
So if you are not testing FAULT is defined but has no defined value. So nothing is expanded.
Aug 8 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.