0

I have a code in c that contains numerous compilation switches. Some are simple like:

#if ENABLE
   // source code
   ....
   ....
#endif

others are more complex like:

#if (CONFIG_SEL == CONF_A || CONFIG_SEL == CONF_C)
#define LOW_BAND    0
#define HIGH_BAND   1
#endif

#if CONFIG_SEL == CONF_A
    #if UNIT_MODEL == UNIT_A
        #if MODEL_SUBTYPE == SUBTYPE_A
            #include "Config_suba.h"
        #elif MODEL_SUBTYPE == SUBTYPE_B
            #include "Config_subb.h"
        #endif
    #elif UNIT_MODEL == UNIT_B
        #include "Config_unitb.h"
    #elif UNIT_MODEL == UNIT_C
        #include "Config_unitc.h"
    #else 
        #include "Config_defaultunit.h"
    #endif
#elif CONFIG_SEL == CONF_B
    #if X_Y_BAND == Y_BAND
    #include "config_xband.h"
    #else 
    #include "config_yband.h"
    #endif
#elif CONFIG_SEL == C
    #include "Config_c.h"
#elif CONFIG_SEL == D
    #include "Config_d.h"
#endif

I was asked to create new .c and .h file where all those parts of code that are not compiled, do not appear in the new code.

Before writing some code in Python which deletes uncompiled switches, I wanted to ask if you knew of a tool that can already do this operation. I tried searching but without success. Erasing by hand is not an option because this operation must be done on very many projects Thank you

4
  • 1
    But that's how conditional compilation using the preprocessor already works... Only the "true" parts will be in the actual compiled code. The compiler will not even see the parts that are in the "false" parts. For example, if CONFIG_SEL is defined to be equal to C, then only #include "Config_c.h" will be done, nothing else. Commented Jun 13, 2024 at 10:21
  • Try gcc -E ...: "Preprocess only; do not compile, assemble or link." Other compilers probably have something similar. Commented Jun 13, 2024 at 10:23
  • Using the preprocessor would expand all macros and flatten all includes. Which you probably don't want. But you can use the preprocessor to e.g. collect all the macros that are defined. Or collect all the line markers (# line_no file_name) that made it into the result and use those to build your output. Commented Jun 13, 2024 at 10:33
  • What is your actual assignment? Can you please edit your question to copy-paste it, including all requirements and limitations. Commented Jun 13, 2024 at 10:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.