【发布时间】:2019-11-06 04:32:39
【问题描述】:
我正在尝试为我的程序实现 VERSION 宏,这将在某些情况下进行更改。
macro VERSION 是通过 Makefile 定义的(git info 放在那里)并且是一个字符串。 现在我有一组#define'd 开关,我希望 VERSION 反映其中哪些处于打开状态。现在看起来如下(main.h):
#define COMPLEX_DEPOSITION // This is switch. later in code it is used in #ifdef...#endif construction.
#ifdef COMPLEX_DEPOSITION
#define CD "_COMP_DEP" // this is the string I want to put in the end of VERSION
#define VERSION_ VERSION CD
#undef VERSION // this is to suppress 'macro redefinition' warning
#define VERSION VERSION_
#undef VERSION_
#endif
好吧,我遇到了很多错误,其中大部分让我认为 C 预处理器以随机顺序处理文件中的行:(
后来我有一个更复杂的东西打算做VERSION -> VERSION_WLT_GAP_2
#define WIRESLIFETIMES
#ifdef WIRESLIFETIMES
#define GAP 2
#define VERSION_ (VERSION ## "_WLT_GAP_" ## #GAP)
#define VERSION VERSION_
#undef VERSION_
#endif
我不知道该怎么做,如果这可能的话
【问题讨论】:
-
我怀疑最简单的解决方案是
#else #define CD ""- 始终连接相同的宏,但如果不需要,只需将它们定义为空字符串。 -
@SirJoBlack 谢谢,我已经看到了这个问题,它的解决方案对我不起作用。
-
@MSalters 谢谢,这解决了第一个问题,但没有解决第二个问题
-
第二个例子的问题似乎是循环逻辑。
VERSION_是根据VERSION和VERSION is then defined asVERSION_` 定义的。我什至不明白那里的真正意图。
标签: c macros concatenation preprocessor