[英]macro if statement returns error: operator '&&' has no right operand
I am compiling my code on many linux machines, on a specific machine, I receive the following error: 我在许多linux机器上编译我的代码,在特定的机器上,我收到以下错误:
error: operator '&&' has no right operand
The macro code is: 宏代码是:
#if LINUX_VERSION_CODE == KERNEL_VERSION(3,12,49) && KERNEL_PATCH_LEVEL == 11
where LINUX_VERSION_CODE and KERNEL_VERSION are defined in linux sources and KERNEL_PATCH_LEVEL is defined in my Makefile 其中LINUX_VERSION_CODE和KERNEL_VERSION在linux源代码中定义,KERNEL_PATCH_LEVEL在我的Makefile中定义
KERNEL_PATCH_LEVEL :=$(word 1, $(subst ., ,$(word 2, $(subst -, ,$(KERNEL_HEADERS)))))
If i change the code to 2 different lines, like this, it works : 如果我将代码更改为2个不同的行,就像这样, 它可以工作 :
#if LINUX_VERSION_CODE == KERNEL_VERSION(3,12,49)
#if KERNEL_PATCH_LEVEL == 11
...
#endif //KERNEL_PATCH_LEVEL == 11
#endif
Is it possible to still keep it with one #if ? 是否有可能仍然保留一个#if? I use gcc version 4.9.0 (Debian 4.9.0-7)
我使用gcc版本4.9.0(Debian 4.9.0-7)
The following macro does not work: 以下宏不起作用:
#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)
it turns out that the error source is that KERNEL_PATCH_LEVEL
is defined in the makefile but empty. 事实证明,错误源是
KERNEL_PATCH_LEVEL
在makefile中定义但是为空。
In that case, the 2 lines aren't equivalent, since 在这种情况下,2行不等同,因为
#if LINUX_VERSION_CODE == KERNEL_VERSION(3,12,49) && KERNEL_PATCH_LEVEL == 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
. 无论第一次测试的结果是什么,都会评估这两个部分,因此预处理器在遇到
&& == 11
时会遇到语法错误。
But if LINUX_VERSION_CODE == KERNEL_VERSION(3,12,49)
is false, with this construct: 但是如果
LINUX_VERSION_CODE == KERNEL_VERSION(3,12,49)
为false,则使用此构造:
#if LINUX_VERSION_CODE == KERNEL_VERSION(3,12,49)
#if KERNEL_PATCH_LEVEL == 11
...
#endif //KERNEL_PATCH_LEVEL == 11
#endif
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. 你没有输入第一个
#if
所以内部#if
(这是错误的,因为值得#if == 11
)属于一个被预处理器跳过的块,这解释了没有错误。
Note that if KERNEL_PATCH_LEVEL
is not defined, #if
sees that as 0
, that wouldn't have triggered any error. 请注意,如果
KERNEL_PATCH_LEVEL
,则#if
将其视为0
,这不会触发任何错误。
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) 你可以用这个来防止错误定义的
KERNEL_PATCH_LEVEL
(在Test for empty macro definition中看到,我现在已经添加了一个更好的答案)
#if (KERNEL_PATCH_LEVEL + 0) == 0
#undef KERNEL_PATCH_LEVEL
#define KERNEL_PATCH_LEVEL 0
#endif
so if the macro is defined empty (or is 0), undefine it and define it to a 0
value. 因此,如果将宏定义为空(或为0),则取消定义它并将其定义为
0
值。 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: 您甚至可以检测到(请参阅上面的链接中的答案以了解它是如何工作的)如果它是空的而不是
0
如下所示:
#if (0-KERNEL_PATCH_LEVEL-1)==1 && (KERNEL_PATCH_LEVEL+0)!=-2
#error "KERNEL_PATCH_LEVEL defined empty"
#endif
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.