简体   繁体   English

宏if语句返回错误:运算符'&&'没有右操作数

[英]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.

相关问题 三元运算符作为C宏中的三元运算符的操作数 - Ternary operator as operand of ternary operator in C macro 如何解决“右手操作数无效”错误? - How to solve 'right-hand operand has no effect' error? 逻辑运算符的右手操作数 || 由于调用 function(MISRA-C 2012Rule 13.5)而具有持续的副作用 - The right hand operand of a logical operator || has persistent side effects because of calling function (MISRA- C 2012Rule 13.5) 警告:逗号右手操作数无效gcc 4.4.7 - warning: right-hand operand of comma has no effect gcc 4.4.7 奇怪的“ asm”操作数具有不可能的约束错误 - Strange 'asm' operand has impossible constraints error 如何使用三元运算符创建一个无任何操作数的#define宏? - How do I create #define macro with a ternary operator with one operand that does nothing? 条件运算符中的“错误:左值需要作为赋值的左操作数” - “error: lvalue required as left operand of assignment” in conditional operator 宏定义的结构变量大小返回“常量表达式中不允许使用此运算符” - Struct vars size defined by macro returns "this operator is not allowed in a constant expression" sizeof运算符中操作数的评估 - evaluation of operand in sizeof operator 在sizeof()运算符中作为操作数地址 - Address as a operand in sizeof() operator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM