Reputation: 2987
I am programming in C using CodeBlocks in Windows with the MingGW compiler that comes bundled with the most recent version. I am trying to get some compiler directives to work to demonstrate conditional compilation.
Below is my code. However, it would seem code blocks or MinGW does not like the #elif parts. If I set my defined macro value DEBUG_MODE to either 3 or 2 neither of the #elif structures appear to work.
Also, codeblocks is greying out the code that falls inside both #elif structures. Have I miscomprehended something about these compiler directives, or is it that not all versions of C support #elif? I know I can solve if just by nesting #if and #else structures, but Id like to know if #elif should work this way. Code below.
Ok so initially I made a schoolboy error and got my conditional logic around the wrong way. I have fixed it now but for the sake of completeness here it is.
Amended code below Codeblocks now behaves as I expect, mostly. The code colouring is off but functionally as I would expect.
#include <stdio.h>
#include <stdlib.h>
#define DEBUG_MODE 3
char * trace;
char * traceDesc;
int main()
{
trace = "main method normal start";
traceDesc = "Main method runs the body of the program to demonstrate compiler directives #if and #elif ";
#if DEBUG_MODE <= 3
printf("Program Begun!\n");
#elif DEBUG_MODE <= 2
printf("trace message %s :", trace);
#elif DEBUG_MODE <= 1
printf("Description message %s :", traceDescr);
#endif
return 0;
}
Upvotes: 0
Views: 274
Reputation: 123478
If I set my defined macro value DEBUG_MODE to either 3 or 2 neither of the #elif structures appear to work.
That's because you say:
#if DEBUG_MODE >= 1
...
#elif
The condition was true and none of the subsequent blocks would be executed.
Depending upon what you're trying to achieve, you might want to say:
#if DEBUG_MODE >= 1
...
#endif
#if DEBUG_MODE >= 2
...
#endif
#if DEBUG_MODE >= 3
...
#endif
Upvotes: 1