首页 > 解决方案 > 从 C 中的字符串 var 执行 #if 条件的其他方法

问题描述

我正在尝试执行字符串 var“str”中给出的条件。
我试图将它放在宏定义中,但它不起作用。
不像,当条件像在“CONDITION_DEF”中一样直接定义时。


    #define CONDITION_STR str
    #define CONDITION_DEF defined A && defined B
    #define A 1
    #define B 1

int main(){ char *str = "defined A && defined B"; #if CONDITION_STR printf("Condition from str: A and B are defined"); #endif
#if CONDITION_DEF printf("Condition from define: A and B are defined"); #endif }

输出:

"Condition from define: A and B are defined"

条件放置在字符串 var 中,以便我可以在运行时更改它。
还有其他方法可以从字符串变量执行#if 条件吗?
谢谢!

标签: cc-preprocessor

解决方案


这是不可能的,因为预处理是在 C 代码的实际编译之前完成的。预处理器对 C 和 C 变量一无所知。


推荐阅读