有些业务场景,当使用C语言的宏进行隔离时,想做到根据宏的字符串来做一些比较处理,可以如下实现;
#include <stdio.h>
#define CM_PRODUCT "CMV888" // 定义宏字符串
int main() {
#if defined(CM_PRODUCT)
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || (defined(__cplusplus) && __cplusplus >= 201103L)
#if (strcmp(CM_PRODUCT, "CMV888") == 0) || (strcmp(CM_PRODUCT, "CMV666") == 0)
printf("CM_PRODUCT is either \"CMV888\" or \"CMV666\"\n");
#else
printf("CM_PRODUCT is not \"CMV888\" or \"CMV666\"\n");
#endif
#else
printf("C99 or C++11 support is required for string comparison\n");
#endif
#else
printf("CM_PRODUCT macro is not defined\n");
#endif
return 0;
}