【发布时间】:2023-04-05 03:29:02
【问题描述】:
我正在处理的程序有问题。我正在尝试在 --help 中显示是否编译了哪些功能。但是,其中有很多,“正常”的方式太冗长了。例如:
#ifdef HAVE_FOO
static const bool have_foo = true;
#else
static const bool have_foo = false;
#endif
printf("Support for foo: %s\n", have_foo ? "yes" : "no");
现在,由于我基本上必须对每个功能都执行此操作,因此会出现很多行,这是我不想要的。
所以我想我会为它写一些宏:
#define _SUPP(X) #ifdef HAVE_##X \
static const bool _##X##_SUPP = true; \
#else \
static const bool _##X##_SUPP = false; \
#endif
#define _PRINTSUPP(var, name, desc) printf("\t%s - %s: %s\n", name, desc, _##var##_SUPP ? "yes" : "no")
但是,这里有一个问题。宏将扩展为单行,预处理器对此感到窒息。有没有办法生成一个中间有实际换行符的宏,或者是否可以在一行上评估 #ifdef?
【问题讨论】:
标签: c macros c-preprocessor