-4

I know there is already a similar post to mine (C++ `ifdef` with concatenation of macros values) but the post is pretty old and the solution provided does not work for me, because I cannot set the define I want to check. So I hope someone can help me.

The problem is that I want to make an ifdef of a concatenation of two a define with a fixed text.

Imagine the following code

#define ENABLE_MODULE_1

enum Modultype
{
    MODULE_1,
    MODULE_2
};

#define MODULE MODULE_1


int main()
{
    #ifdef ENABLE_ ## MODULE
    printf("NAME defined");
    #else
    printf("NAME not defined");
    #endif
    return 0;
}

So I basically want to check if ENABLE_MODULE_1 is defined based on the MODULE define.

I hope someone can help me. Thanks!

EmbedEngineer
  • 93
  • 2
  • 7

2 Answers2

0

This is not possible. Preprocessor macros only expand once, and only when not in another preprocessor statement. Along with that, concatenations can only be used within a #define statement.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
0

You cannot do precisely what you want because #ifdef operates on an identifier, not a preprocessor expression, and the ## operator can only be used in the replacement text of a macro definition.

If you are willing to define ENABLE_MODULE_1 (if defined at all) with a non-zero constant as its replacement text, you can achieve something similar to what you want as follows:

#include <stdio.h>

#define XENABLED(x)         ENABLE_ ## x
#define ENABLED(x)          XENABLED(x)

#define XSTR(x)             #x
#define STR(x)              XSTR(x)

#define ENABLE_MODULE_1     1   /* enabled */
#define ENABLE_MODULE_3     0   /* not enabled */

#define MODULE              MODULE_1
#define OTHER               MODULE_2
#define ANOTHER             MODULE_3

int main(void)
{
#if ENABLED(MODULE)
    printf("%s enabled\n", STR(MODULE));
#else
    printf("%s not enabled\n", STR(MODULE));
#endif
#if ENABLED(OTHER)
    printf("%s enabled\n", STR(OTHER));
#else
    printf("%s not enabled\n", STR(OTHER));
#endif
#if ENABLED(ANOTHER)
    printf("%s enabled\n", STR(ANOTHER));
#else
    printf("%s not enabled\n", STR(ANOTHER));
#endif
#if ENABLED(UNDEFINED)
    printf("%s enabled\n", STR(UNDEFINED));
#else
    printf("%s not enabled\n", STR(UNDEFINED));
#endif
    return 0;
}

The above program produces the following output:

MODULE_1 enabled
MODULE_2 not enabled
MODULE_3 not enabled
UNDEFINED not enabled
Ian Abbott
  • 15,083
  • 19
  • 33
  • Thanks for the suggestion! Unfortunatly I cannot set the `ENABLE_MODULE_1` as it is given in a configuration file which I do not have access to. I was therefore looking for a general soluation, so that I do not have to check manually for each ENABLE, but apparently this cannot be done. Many thanks for the effort anyway. – EmbedEngineer Jun 27 '19 at 07:09
  • @EmbedEngineer I've added a variant using `defined()` that might help you. – Ian Abbott Jun 27 '19 at 09:38
  • @IanAbbott Note that the solution using `defined(...)` invokes undefined behaviour as far as the standard is concerned: “If the token `defined` is generated as a result of [macro replacement in the controlling expression of `#if` or `#elif`], the behavior is undefined” (C89 6.8.1, C99 and C11 6.10.1). Yes, in standard C you can invoke undefined behaviour _during compilation_; yes, that’s stupid. – Alex Shpilkin Jun 05 '21 at 21:38
  • @AlexShpilkin Thanks for the information. I'll remove that part. – Ian Abbott Jun 06 '21 at 10:41