473,395 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Conditionally compiling C++ macros

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
3 1748
weaknessforcats
9,208 Expert Mod 8TB
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
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
yasmin
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

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

Similar topics

7
by: Steven T. Hatton | last post by:
Is there anything that gives a good description of how source code is converted into a translation unit, then object code, and then linked. I'm particularly interested in understanding why putting...
10
by: jeroendeurinck | last post by:
Hi all, I'm a newbe, so sorry if this question would be inappropriate here. Nevertheless I try. --- Suppose I have a class CTraffic in which several objects of class CVehicle move around....
10
by: SueB | last post by:
I currently have a 'mail-merge' process in my Access db project. It generates custom filled out Award Certificates based on an SQL SELECT statement in a VBA routine invoked by clicking on a...
6
by: Arthur | last post by:
I had two Visual Studio .NET C++ solutions that I combined into one. These project solutions were very similar. In fact, the two solutions were sharing many files. Now that they are one...
2
by: Jens Weiermann | last post by:
Hi, I need to conditionally disable a html (not web forms) button control. The condition is to be evaluated server-side. What is the smartest way to do this? I'm now using a literal server-side...
2
by: Felix | last post by:
Hello, I use a #define like WITH_MYSTUFF to conditionally compile code within #ifdef WITH_MYSTUFF. I would now like the same define to conditionally link with a library. In the linker settings...
4
by: bubulle | last post by:
Hi, all. Here is my problem: Let's say i have table1 with columns a,b,c and table2 with cols x,y,z. Some of columns contain the same type of data from one table to the other, but others are...
1
by: kaens | last post by:
So, I have a class that has to retrieve some data from either xml or an sql database. This isn't a problem, but I was thinking "hey, it would be cool if I could just not define the functions for...
13
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
When I'm writing my own code, compiling it and testing it out as I go along, I usually compile as follows: gcc *.c -ansi -pedantic -Wall -o executable If I'm using 3rd-party libraries, I'll...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.