gpt4 book ai didi

c - GCC Wunused-variable 对已使用的变量发出警告

转载 作者:行者123 更新时间:2023-11-30 20:53:00 25 4
gpt4 key购买 nike

我使用的是 GCC 版本 8.2在几段代码中,我使用了小函数。我对每一项功能都进行了测试(即 Unity 框架测试)。测试被定义​​为 #define 宏,测试非常具体的事情。例如,如果一个数字是正数。

现在,当使用 -Wextra 标志编译代码时,我收到有关未使用变量的警告,尽管我在定义的宏上使用它们。

问题是,GCC 无法将宏识别为使用变量,还是我遗漏了什么?

示例:

    #define compare(a,b) ( ((a) == (b)) ? 1 : 0 )
...
void f() {
int a;
a = f1();
if(compare(a,123))
printf("It works");
}

在这种情况下,GCC 会警告未使用的变量 a,尽管它正在被宏使用(除了由函数 f1() 赋予值之外)。

最佳答案

情况并非如此,至少在您提供的示例中是这样。这是Minimal, Complete, and Verifiable演示:

#include <stdio.h>

#define compare(a,b) ( ((a) == (b)) ? 1 : 0 )

int f1() {
return 42;
}

void f() { // your code
int a;
a = f1();
if (compare(a, 123))
printf("It works");
}

int main(int argc, char *argv[]) {
f();
return 0;
}

当使用 gcc 8.2 或 7.3 与 gcc -Wall -Wunused 进行编译时(是的,这是多余的),没有警告或错误。

关于c - GCC Wunused-variable 对已使用的变量发出警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54243687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com