【发布时间】:2017-11-10 02:15:44
【问题描述】:
考虑以下 - 我想检查 #if #endif 是否
令牌在代码中的某处定义。
我正在使用 CONCAT(input) 宏,它应该粘合我要检查的令牌的常量和变化部分。
很遗憾,下面介绍的方法会导致编译错误:
error: missing binary operator before token "("
我找到了可以放在#if #endif 块内的表达式:
https://gcc.gnu.org/onlinedocs/cpp/If.html#If
显然它指出:
宏。表达式中的所有宏都在表达式值的实际计算开始之前展开。
原来(CONCAT(test))应该被解析,但事实并非如此。
是否有任何解决方法允许在条件编译块中正确解析连接的令牌名称?
#include <stdio.h>
#define CONCAT(input) string##input
#define stringtest 1
int main(void)
{
#if defined(CONCAT(test)) && (CONCAT(test)==1)
printf("OK");
#else
printf("NOT");
#endif
return 0;
}
【问题讨论】:
-
我认为你不能那样做。
标签: c macros c-preprocessor conditional-compilation define-syntax