宏if语句返回错误:运算符'&&'没有正确的操作数 [英] macro if statement returns error: operator '&&' has no right operand
问题描述
我正在许多Linux机器上编译代码,在一台特定机器上,我收到以下错误:
I am compiling my code on many linux machines, on a specific machine, I receive the following error:
error: operator '&&' has no right operand
宏代码为:
#if LINUX_VERSION_CODE == KERNEL_VERSION(3,12,49) && KERNEL_PATCH_LEVEL == 11
其中LINUX_VERSION_CODE和KERNEL_VERSION在linux源文件中定义,而KERNEL_PATCH_LEVEL在我的Makefile文件中定义
where LINUX_VERSION_CODE and KERNEL_VERSION are defined in linux sources and KERNEL_PATCH_LEVEL is defined in my Makefile
KERNEL_PATCH_LEVEL :=$(word 1, $(subst ., ,$(word 2, $(subst -, ,$(KERNEL_HEADERS)))))
如果我将代码更改为2个不同的行,例如有效:
If i change the code to 2 different lines, like this, it works:
#if LINUX_VERSION_CODE == KERNEL_VERSION(3,12,49)
#if KERNEL_PATCH_LEVEL == 11
...
#endif //KERNEL_PATCH_LEVEL == 11
#endif
是否可以将其与#if一起保存?
我使用的是gcc版本4.9.0(Debian 4.9.0-7)
Is it possible to still keep it with one #if ? I use gcc version 4.9.0 (Debian 4.9.0-7)
以下宏无效:
#if (LINUX_VERSION_CODE == KERNEL_VERSION(3,12,49)) && (KERNEL_PATCH_LEVEL == 11)
#if ((LINUX_VERSION_CODE == KERNEL_VERSION(3,12,49)) && (KERNEL_PATCH_LEVEL == 11))
#if defined(KERNEL_MAJOR) && defined(KERNEL_MINOR) && defined(KERNEL_MICRO) && defined(KERNEL_PATCH_LEVEL) && defined(KERNEL_VERSION) &&
defined(LINUX_VERSION_CODE) && \
(LINUX_VERSION_CODE == KERNEL_VERSION(3,12,49)) && (KERNEL_PATCH_LEVEL == 11)
推荐答案
事实证明,错误源是在makefile中定义了 KERNEL_PATCH_LEVEL
但为空。
it turns out that the error source is that KERNEL_PATCH_LEVEL
is defined in the makefile but empty.
在这种情况下,两行不是等于t,因为
In that case, the 2 lines aren't equivalent, since
#if LINUX_VERSION_CODE == KERNEL_VERSION(3,12,49) && KERNEL_PATCH_LEVEL == 11
无论第一个测试的结果如何,都对这两个部分进行评估,因此遇到&&时,预处理器偶然发现语法错误== 11
。
evaluates both parts no matter what the the outcome of the first test is, so the preprocessor stumbles on the syntax error when meeting && == 11
.
但是如果 LINUX_VERSION_CODE == KERNEL_VERSION(3,12,49)
是错误的,具有以下构造:
But if LINUX_VERSION_CODE == KERNEL_VERSION(3,12,49)
is false, with this construct:
#if LINUX_VERSION_CODE == KERNEL_VERSION(3,12,49)
#if KERNEL_PATCH_LEVEL == 11
...
#endif //KERNEL_PATCH_LEVEL == 11
#endif
您没有输入第一个 #if
,因此输入的是内部 #if
(这是错误的,因为价值 #if == 11
)属于预处理器跳过的一个块,这说明没有错误。
you're not entering the first #if
so the inner #if
(which is wrong since worth #if == 11
) belongs to a block which skipped by the preprocessor, which explains that there's no error.
请注意,如果未定义 KERNEL_PATCH_LEVEL
,则 #if
会将其视为 0
,那不会触发任何错误。
Note that if KERNEL_PATCH_LEVEL
is not defined, #if
sees that as 0
, that wouldn't have triggered any error.
您可以防止定义不明确的 KERNEL_PATCH_LEVEL
与此(参见测试空宏定义,已经添加了更好的答案)
you can protect against ill-defined KERNEL_PATCH_LEVEL
with this (seen in Test for empty macro definition, I have added a better answer now)
#if (KERNEL_PATCH_LEVEL + 0) == 0
#undef KERNEL_PATCH_LEVEL
#define KERNEL_PATCH_LEVEL 0
#endif
因此,如果宏定义为空(或为0),请取消定义并将其定义为 0
值。您甚至可以检测到它是否为空(而不是 0
这样)(
so if the macro is defined empty (or is 0), undefine it and define it to a 0
value. You could even detect (see my answer in the link above to understand how it works) if it's empty instead of 0
like this:
#if (0-KERNEL_PATCH_LEVEL-1)==1 && (KERNEL_PATCH_LEVEL+0)!=-2
#error "KERNEL_PATCH_LEVEL defined empty"
#endif
这篇关于宏if语句返回错误:运算符'&&'没有正确的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!