Incorrect compiling with 'Release' and 'Debug' Targets

Hello everyone!

I've found myself in a spot of bother with Keil uVision 5.24.2.0 using ARM Compiler v5.06 update 5...although the build issues I have don't appear specific to these versions. Let me explain...

In my Keil project I have two targets: 'Release' and 'Debug'. The goal is to globally enable/disable various debug options and change formatted output simply by selecting the appropriate build target and compiling.

Here is how I tried to make it work:

- In 'Options for Target', C/C++ tab, 'Preprocessor Symbols' Define section, I have defined: 'DEBUG' for the Debug target 'noDEBUG' for the Release target all other settings are identical between the Debug and Release targets.

- I have a header file 'debug_config.h' in which I have enable/disable various debug options like so:

#ifndef __DEBUG_CONFIG_H
#define __DEBUG_CONFIG_H

#ifdef DEBUG
#define DEBUG_BUILD 1
#else
#define DEBUG_BUILD 0
#endif

#if ( DEBUG_BUILD == 1 )
#define TEST_MODE_RECOGNIZER 1
#endif

#endif

- In main.c and other source code files (which all #include "debug_config.h") I refer only to DEBUG_BUILD and TEST_MODE_* like this:

printf("IronMan Gen3.7 (%s)\r\n", (DEBUG_BUILD == 1) ? "Debug" : "Release");

#if ( TEST_MODE_RECOGNIZER == 1 )
printf("Jarvis 8.9b Test Mode\r\n");
#endif

With all that in place, if I 'Clean Targets', select 'Debug' target, then Build I will get the following output:

IronMan Gen3.7 (Debug)
Jarvis 8.9b Test Mode

Perfect!

Now I switch to 'Release' target and without doing 'Clean Targets' I do Build. I find main.c is NOT compiled! Why not? Other files which don't #include "debug_config.h" and don't mention DEBUG, DEBUG_BUILD, or TEST_MODE_RECOGNIZER are compiled. Because main.c is not recompiled I get the same output as above! What I want of course is:

IronMan Gen3.7 (Release)

I understand that I did not change main.c source code, so from compiler perspective it thinks perhaps it doesn't need to retranslate it. However, I selected different Target, which had different Defines, so I expected that it would recompile main.c like it did with other files.

I note there is a 'Undefine' section in the Target C/C++ Preprocessor Symbols, but it is grayed out. I can't select or enter anything there, otherwise I would 'Undefine DEBUG' for the Release target.

So I am stumped...

I hope this provides clear example of what I would like to do. Obviously there's something I've done wrong or misunderstood...

Thanks for any advice or suggestions on this!

Best!

Parents
  • Several issues to note:

    1) this way of conditionally defining a preprocessor switch macro:

    #if ( DEBUG_BUILD == 1 )
    #define TEST_MODE_RECOGNIZER 1
    #endif
    


    is a not-quite-optimal fit for this kind of testing the condition:

    #if ( TEST_MODE_RECOGNIZER == 1 )
    

    If your condition is #if instead of #ifdef/#if defined(), you should usually always #define the macro in question (to 1 or 0, accordingly). It'll still work the way you do it, because undefined macros will expand to 0 as a last-ditch effort, but you'll get flak from most code checking tools for that style.

    2) having a header that relies on:
    which all #include "debug_config.h"
    is riskier than it needs to be. It's better to move all those individual, conditionally defined macros directly into the IDE instead, if only because it saves you from silly problems in case you ever forget putting that #include "debug_config.h" in one header or .c file. And it'll save you from warnings about including headers that the code doesn't actually use.

    3) is your real issue:
    However, I selected different Target, which had different Defines, so I expected that it would recompile main.c like it did with other files.
    If you wanted that, you had better specify separate output folders for the two targets. It's arguably a bug that uVision doesn't automatically do that as soon as you set up the second target in the same project.

  • Thank you very much, Hans-Bernhard, your comment #3 helped me resolve my problem! As soon as I created an output folder for each target I got the behavior I was aiming for.

    I'm still puzzled why many other .c files -- e.g. those with GPIO config with no dependency on debug_config.h or any of the #defines -- were recompiled for the different Target build, but main.c was not.

    Also, I'm unsure why the 'Undefine' preprocessor symbol dialog box is grayed out at the Target level, but enabled at the Group and File levels. This option (discussed here http://www.keil.com/support/man/docs/uv4/uv4_dg_adscc.htm) says 'Undefine: Clears previous Define assignments that are entered in the options dialog of a higher Target or Group level.'

    I had made my 'Debug' target the highest level, where I Defined 'DEBUG'. My 'Release' target is the second (and last) target. According to website above, it appears I should be able to Undefine 'DEBUG' for the (lower level) Release target. But, the dialog box is grayed out...

    I appreciate your other comments, also. Here is my thoughts.

    1) you should usually always #define the macro in question (to 1 or 0, accordingly)
    Good point! I will update my code to:

    #if ( DEBUG_BUILD == 1 )
    #define TEST_MODE_RECOGNIZER 1
    #else
    #define TEST_MODE_RECOGNIZER 0
    #endif
    

    Hmmm...it seems you make this recommendation based on my choice to use #if instead of #ifdef/#if defined(). If so, why does this apply in my case, but not if I opted for #ifdef?

    To be clear, if I had:

    //#define TEST_MODE_RECOGNIZER 1
    
    #ifdef TEST_MODE_RECOGNIZER
    printf("Jarvis 8.9b Test Mode\r\n");
    #endif
    

    Then wouldn't the undefined macro TEST_MODE_RECOGNIZER still expand to 0? I don't see how #ifdef would be more-optimal than my not-quiet-optimal use of #if. I feel I'm missing something. Could you explain a little further?

    2) move all those individual, conditionally defined macros directly into the IDE instead
    Do you mean define the macros in the 'Options for...' C/C++ preprocessor symbols at the Target, Group or File level?

    It seems cumbersome to define many macros in the Defines dialog box like: TEST_MODE_RECOGNIZER=1, TEST_MODE_JARVIS=2, etc. I don't think it will be easy to find/check/change one macro in a long list this way...seems like I would have to set the Defines on 'Options for File' for many different files to make it manageable. And then, perhaps I'd have to repeat the process for each target.

    However, if that is the recommended way, how would I conditionally define a macro based on an earlier defined macro? That is, how to do the equivalent of:

    #ifdef DEBUG
    #define DEBUG_BUILD 1
    #else
    #define DEBUG_BUILD 0
    #endif
    

    If this is not at all what you meant, could you explain further? Much appreciated!

    Best!

Reply Children
More questions in this forum