|
-
August 30th, 2006, 04:46 AM
#1
macro problem
Code:
#define SOMEPRINTF 1 ? (void)0 : printf // 0 to enable and 1 to disable
this does not pose me a problem ( for 1 and 0) but if i change it this way
Code:
#define SOMEPRINTF 1 ? printf : (void)0 // 1 to enable and 0 to disable
the compiler shows me error : called object is not a function
i am using gcc 2.95
-
August 30th, 2006, 05:00 AM
#2
Re: macro problem
use -save-temps compiler option and check what your macro expands to. I bet it is something nasty.
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
August 30th, 2006, 05:05 AM
#3
Re: macro problem
You cannot use 0 as no-op and the macro will be expanded out. In the first case you will get your code expanded to
Code:
1 ? (void)0 : printf( args )
which makes sense because you can pass args to printf but in the latter case it is expanded to
Code:
1 ? printf : (void)0( args )
which won't compile.
Better would be to declare a no-op printf thus:
Code:
int no_op( ... )
{
// does nothing
}
Now get your macro to return a function to a pointer and call it on the args, so you'll need brackets thus:
Code:
#define SOMEPRINTF (1 ? printf : no_op )
and that might work.
-
August 30th, 2006, 05:11 AM
#4
Re: macro problem
If I diable it or enable there is no problem with the first declaration.
Code:
#define SOMEPRINTF 1 ? (void)0 : printf // 0 to enable and 1 to disable
This compiles fine. Why is it so? Does it not expand to the same nasty expansion as
-
August 30th, 2006, 05:13 AM
#5
Re: macro problem
 Originally Posted by NMTop40
Better would be to declare a no-op printf thus:
Code:
int no_op( ... )
{
// does nothing
}
Now get your macro to return a function to a pointer and call it on the args, so you'll need brackets thus:
Code:
#define SOMEPRINTF (1 ? printf : no_op )
and that might work.
This will have an extra call. I need to complelety disable the printf. Well, actually those are debug calls which I would like to disable when compiling for release. Any other alternate solutions?
-
August 30th, 2006, 05:21 AM
#6
Re: macro problem
You can try the following example.
Code:
#ifdef TURNON_DEBUG
#define SOMEPRINTF printf
#else
#define SOMEPRINTF
#endif
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
August 30th, 2006, 05:25 AM
#7
Re: macro problem
 Originally Posted by Kheun
You can try the following example.
Code:
#ifdef TURNON_DEBUG
#define SOMEPRINTF printf
#else
#define SOMEPRINTF
#endif
That would leave the arguments floating around in space. I think that's allowed, but you could define SOMEPRINTF to be no_op as I defined it.
-
August 30th, 2006, 05:27 AM
#8
Re: macro problem
 Originally Posted by kumaresh_ana
This will have an extra call. I need to complelety disable the printf. Well, actually those are debug calls which I would like to disable when compiling for release. Any other alternate solutions?
The compiler would see no_op is empty and strip it out. If this is C++ or C99 you can use the inline keyword to advise the compiler to do just that.
-
August 30th, 2006, 05:42 AM
#9
Re: macro problem
 Originally Posted by NMTop40
That would leave the arguments floating around in space. I think that's allowed, but you could define SOMEPRINTF to be no_op as I defined it.
Yes, it is allowed. In fact, the floating arguments are being optimized out from the compiled code.
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
August 30th, 2006, 06:21 AM
#10
Re: macro problem
No. My compiler throws error upon seeing floating arguements.
-
August 30th, 2006, 07:20 AM
#11
Re: macro problem
Would you give me a sample of the code and what compiler are you using? It is working on VC and GCC (if I recalled correctly).
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
August 30th, 2006, 07:28 AM
#12
Re: macro problem
I am using gcc 2.95 but not intel platform. Its an embedded processor.
Here are some samples.....
Code:
SOMEPRINTF("kum debug-> xx calling yy");
SOMEPRINTF("kum debug-> xx val is %d", xx);
-
August 30th, 2006, 08:22 PM
#13
Re: macro problem
My linux machine is installed with GCC 3.2.3 and your sample compiles fine without any error. The only way I can see the warning messages only if I set to higher warning level. e.g. gcc test.c -W -o test.out.
test.c: In function `main':
test.c:19: warning: left-hand operand of comma expression has no effect
test.c:18: warning: statement with no effect
test.c:19: warning: statement with no effect
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
August 30th, 2006, 11:53 PM
#14
Re: macro problem
I think I need to change the compiler. Thanks for your help guys.
-
September 1st, 2006, 01:23 AM
#15
Re: macro problem
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|