how to compare in #if condition of preprocessor for string

Viewed 181

Hi my c code need to consider block of code depending on

#if <condition>
 //code block to execute if conditions matches
#endif

Now problem is in my shell script which I run before I compile c code I set ML_ARCH=r8. I can do check in Makefile as below:

ifeq ($(ML_ARCH), r8)
endif

But for C code how to compare variable against string which is mix of char and number, in preprocessor #if

1 Answers

how to compare in #if condition of preprocessor for string

It is not possible to compare strings in preprocessor.


You can add something unique to r8 to make it longer and define it to a number. You could just #define r8 1, but r8 is a bit short and could be break random code.

// #define ML_ARCH   r8

#define ARCH_r8               1
#define ARCH_some_other_arch  2
#define ARCH_etc              3

#define XADD_ARCH(a) ARCH_##a
#define ADD_ARCH(a)  XADD_ARCH(a)    
#define IS_ARCH(a)   (ADD_ARCH(ML_ARCH) == ARCH_##a)

// #if XCONCAT(ARCH_, ML_ARCH) == ARCH_r8
#if IS_ARCH(r8)

Overall, consider doing -D ML_ARCH=ARCH_r8 and then just #if ML_ARCH == ARCH_r8.

Related