cancel
Showing results for 
Search instead for 
Did you mean: 

Why is my #ifdef not working correctly?

gsieb.1
Associate II

I have a __BOOTLOADER symbol defined for my project as shown in the attached image

0693W00000CzdxqQAB.jpgI want to display firmware version based on the defined symbol so I have an #ifdef statement to select the correct string. The IDE displays the source code correctly: the greyed/ungreyed sections are displayed as expected.

0693W00000Czdy5QAB.jpgBut when I run the program, GB_FW_REV[] = "5.26.1" instead of "5.26.B".

What am I doing wrong?

Thanks!

7 REPLIES 7
TDK
Guru

Is this in a C file or C++? Your #define is in C.

Make sure you're on the same build configuration as you have the #define set up in. Release or Debug.

Do a sanity check and put some invalid syntax in the part you think it should be running to see if the compiler is compiling what you think it is.

If you feel a post has answered your question, please click "Accept as Solution".
gsieb.1
Associate II

Thanks for responding!

This is in a C file.

It's as if, at the time this file is compiled, none of those symbols are known yet, so it defaults to the last line.

But I forgot to mention, I did do a sanity check by commenting all the other #elifs and just leaving

#ifdef __BOOTLOADER

const u08 GB_FW_REV[MAX_FW_LEN] = {"5.26.B"};

#endif

When i do that, GB_FW_REV[] = "5.26.B" as expected!

TDK
Guru

You can add a compiler flag to output all known defines. If the simpler version works, I'm guessing something else is going on.

You can also verify the define gets set correctly by looking at the command line compilation of that file.

If you feel a post has answered your question, please click "Accept as Solution".
Cartu38 OpenDev
Lead II

@gsieb.1​is #elseif leading to same ?

ONadr.1
Senior III

I am not absolutely sure, but i think, that #elif is possibe to use after #if, not after #ifdef.

gsieb.1
Associate II

Thanks everyone for responding!

@Cartu38 OpenDev​ : I did not know that "__" (2 underscores) plus an upper case char is something reserved by the compiler (I'm not clear on that rule), but to make sure it's not causing a problem, I replaced "__BOOTLOADER" to "BOOTLOADER" throughout the code with no effect.

Also, "#elseif" does not seem to be supported...?

@ONadr.1​ : I tried changing the format from #ifdef <SYMBOL1> #elif <SYMBOL2>... to #if defined(<SYMBOL1>) #elif defined (<SYMBOL2>) ... with no effect

@TDK​ : I could not find a flag that will force the compiler or linker to generate a list of #define symbols. Do you happen to know how?

So with my current settings and code, (changes made based on all advice received) the result at runtime is still GB_FW_REV[] = "5.26.1" even though it should clearly not be.

0693W00000D01AxQAJ.png 0693W00000D010xQAB.png 

If I remove the preprocessor symbol "BOOTLOADER" from the settings and instead add" #define BOOTLOADER" in my code, then the result is GB_FW_REV[] = "5.26.B" as expected.

gsieb.1
Associate II

Solved!

Stupid mistake: the build configuration that I had selected as the active build configuration was not the same build configuration that I was debugging.

@Cartu38 OpenDev​ , @TDK, @ONadr.1​ , thanks again for responding.