Click to See Complete Forum and Search --> : macro problem


kumaresh_ana
August 30th, 2006, 04:46 AM
#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

#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

Hobson
August 30th, 2006, 05:00 AM
use -save-temps compiler option and check what your macro expands to. I bet it is something nasty.

NMTop40
August 30th, 2006, 05:05 AM
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

1 ? (void)0 : printf( args )

which makes sense because you can pass args to printf but in the latter case it is expanded to

1 ? printf : (void)0( args )

which won't compile.

Better would be to declare a no-op printf thus:

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:

#define SOMEPRINTF (1 ? printf : no_op )

and that might work.

kumaresh_ana
August 30th, 2006, 05:11 AM
If I diable it or enable there is no problem with the first declaration.

#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

(void)0( args )

kumaresh_ana
August 30th, 2006, 05:13 AM
Better would be to declare a no-op printf thus:

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:

#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?

Kheun
August 30th, 2006, 05:21 AM
You can try the following example.



#ifdef TURNON_DEBUG

#define SOMEPRINTF printf

#else

#define SOMEPRINTF

#endif

NMTop40
August 30th, 2006, 05:25 AM
You can try the following example.



#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.

NMTop40
August 30th, 2006, 05:27 AM
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.

Kheun
August 30th, 2006, 05:42 AM
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.

kumaresh_ana
August 30th, 2006, 06:21 AM
No. My compiler throws error upon seeing floating arguements.

Kheun
August 30th, 2006, 07:20 AM
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).

kumaresh_ana
August 30th, 2006, 07:28 AM
I am using gcc 2.95 but not intel platform. Its an embedded processor.

Here are some samples.....

SOMEPRINTF("kum debug-> xx calling yy");

SOMEPRINTF("kum debug-> xx val is %d", xx);

Kheun
August 30th, 2006, 08:22 PM
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

kumaresh_ana
August 30th, 2006, 11:53 PM
I think I need to change the compiler. Thanks for your help guys.

world
September 1st, 2006, 01:23 AM
hey kumar



you can go for devc++

http://www.bloodshed.net/devcpp.html