- May 11, 2008
- 22,599
- 1,473
- 126
Hello everybody, i have not fully searched (bit short on time & teeny bit tired) this time and i was hoping for a short answer. I asked several people and nobody seemed to know if it was possible or not. I thought, i would be best of when asking the question here.
This is what i want to have, and i would like to know if it is possible :
Is it possible to have conditional preprocessor statements in a macro define ?
Example :
GNU does not seem to have a problem with it.
However, there is one problem when using this macro, see code...
And the compiler does not except it because it does not know what kind of variable text is.
These are the errors that i get :
Normally i would chew myself into it but i am a bit weary at the moment.
What am i doing wrong ?
Am i expecting something impossible from the preprocessor ?
I really want to avoid the use of variables and just use strings stored in flash memory as text.
For example as this way that is often used :
The idea is to create debug code into the normal code without using any instructions for the debug code when not used. I would have a general debug.h file :
This way i would only have to add the debug.h header file, include it and use the macro in all c files. When i uncomment the define DEBUG_FUNCTION_IS_YES all debug messages are enabled.
Of course i would have to add prototypes and more but that is not an issue.
Thank you in advance.
This is what i want to have, and i would like to know if it is possible :
Is it possible to have conditional preprocessor statements in a macro define ?
Example :
Code:
[COLOR="Magenta"]#define DebugTextOut_m (text, variable) { \
#ifdef DEBUG_FUNCTION_IS_YES \
FunctionNumber1(); \
PrintText(text,variable); \
FunctionNumber2(); \
#endif \
}[/COLOR]
GNU does not seem to have a problem with it.
However, there is one problem when using this macro, see code...
Code:
[COLOR="Blue"]void[/COLOR] test([COLOR="Blue"]void[/COLOR])
{
DebugTextOut_m([COLOR="Red"]"herp derp %d"[/COLOR],1);
}
And the compiler does not except it because it does not know what kind of variable text is.
These are the errors that i get :
src/usb.c:44: error: 'text' undeclared (first use in this function)
src/usb.c:44: error: (Each undeclared identifier is reported only once
src/usb.c:44: error: for each function it appears in.)
src/usb.c:44: error: 'variable' undeclared (first use in this function)
src/usb.c:44: warning: left-hand operand of comma expression has no effect
src/usb.c:44: error: expected ';' before '{' token
src/usb.c:44: error: stray '#' in program
src/usb.c:44: error: stray '#' in program
Normally i would chew myself into it but i am a bit weary at the moment.
What am i doing wrong ?
Am i expecting something impossible from the preprocessor ?
I really want to avoid the use of variables and just use strings stored in flash memory as text.
For example as this way that is often used :
Code:
PrintText([COLOR="Red"]"herp derp"[/COLOR], data);
The idea is to create debug code into the normal code without using any instructions for the debug code when not used. I would have a general debug.h file :
Code:
[COLOR="YellowGreen"]// Debug.h[/COLOR]
[COLOR="YellowGreen"]// Comment out if not needed.[/COLOR]
[COLOR="Magenta"]#define DEBUG_FUNCTION_IS_YES[/COLOR]
#ifndef _DEBUGMESSENGER
#define _DEBUGMESSENGER
[COLOR="Magenta"]#define DebugTextOut_m (text, variable) { \
#ifdef DEBUG_FUNCTION_IS_YES \
FunctionNumber1(); \
PrintText(text,variable); \
FunctionNumber2(); \
#endif \
}[/COLOR]
#endif
This way i would only have to add the debug.h header file, include it and use the macro in all c files. When i uncomment the define DEBUG_FUNCTION_IS_YES all debug messages are enabled.
Of course i would have to add prototypes and more but that is not an issue.
Thank you in advance.
Last edited:
