问题描述
In file included from /home/omar/Desktop/lovelace/src/main.cpp:22:
/home/omar/Desktop/lovelace/include/stb/stb_image.h: In function ‘void* stbi__load_gif_main(stbi__context*,int**,int*,int)’:
/home/omar/Desktop/lovelace/include/stb/stb_image.h:6778:11: **error: variable ‘out_size’ set but not used** [-Werror=unused-but-set-variable]
6778 | int out_size = 0;
| ^~~~~~~~
/home/omar/Desktop/lovelace/include/stb/stb_image.h:6779:11: **error: variable ‘delays_size’ set but not used** [-Werror=unused-but-set-variable]
6779 | int delays_size = 0;
| ^~~~~~~~~~~
cc1plus: all warnings being treated as errors
make[2]: *** [CMakeFiles/lovelace.dir/build.make:102: CMakeFiles/lovelace.dir/src/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/lovelace.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
然而似乎没有任何问题,因为确实使用了变量:
int out_size = 0;
int delays_size = 0;
memset(&g,sizeof(g));
if (delays) {
*delays = 0;
}
do {
u = stbi__gif_load_next(s,&g,comp,req_comp,two_back);
if (u == (stbi_uc *) s) u = 0; // end of animated gif marker
if (u) {
*x = g.w;
*y = g.h;
++layers;
stride = g.w * g.h * 4;
if (out) {
void *tmp = (stbi_uc*) STBI_REALLOC_SIZED( out,out_size,layers * stride );
if (NULL == tmp) {
STBI_FREE(g.out);
STBI_FREE(g.history);
STBI_FREE(g.background);
return stbi__errpuc("outofmem","Out of memory");
}
else {
out = (stbi_uc*) tmp;
out_size = layers * stride;
}
if (delays) {
*delays = (int*) STBI_REALLOC_SIZED( *delays,delays_size,sizeof(int) * layers );
delays_size = layers * sizeof(int);
}
} else {
out = (stbi_uc*)stbi__malloc( layers * stride );
out_size = layers * stride;
if (delays) {
*delays = (int*) stbi__malloc( layers * sizeof(int) );
delays_size = layers * sizeof(int);
}
} int out_size = 0;
这是编译器错误吗?
编辑:GCC 的版本是 9.3.0
操作系统:Linux Mint 最新版
解决方法
似乎使用 stbi__load_gif_main(...)
会生成警告。这不是编译器的错,也不是你的错。
我复制了这个并得到了相同的结果。
无需深入了解原因,您可以通过编译不标志 -Werror
来解决此问题。
任何可以首先填写警告的原因的人,请填写。
,代码初始化并修改了 out_size
和 delay_size
,但不使用它们(它们只被写入,从不读取)。它出现的一个地方是 STBI_REALLOC_SIZED()
调用,但 STBI_REALLOC_SIZED
是一个这样定义的宏:
#define STBI_REALLOC_SIZED(p,oldsz,newsz) STBI_REALLOC(p,newsz)
所以 oldsz
未使用。
在不使用 -Wunused-variable
(或暗示这一点的 -W
选项之一)和 -Werror
的情况下进行编译是一种不令人满意的解决方案,因为它会在您自己的代码中抑制您可能想要的警告。如果这是普通的库代码而不是头文件中的静态函数(这本身就是一件令人讨厌的事情),那么您可以使用与您自己的代码不同的选项来编译第三方代码。最简单的解决方案是修改 stb_image.h 文件,使其具有:
int out_size = 0; out_size = out_size ;
int delays_size = 0; delays_size = delays_size ;
或
int out_size = 0; (void)out_size ;
int delays_size = 0; (void)delays_size ;
原来标题已经有一个宏:
int out_size = 0; STBI_NOTUSED(out_size) ;
int delays_size = 0; STBI_NOTUSED(delays_size) ;
这将以一种没有实际效果并且可以优化的方式“使用”变量(尽管如果您选择使用这种可怕的头文件滥用,这显然不是问题)。虚拟使用抑制警告。
通常,您可能不想将标头更改为第三方库,但在这种情况下,标头 是 库,因此如果说它是某个库的标头,则不存在不一致的风险。共享对象库。所有使用它的代码都可以看到更改。唯一的问题是,如果您采用更新版本,则可能必须重新实施修复程序。一种解决方案是向作者提交更改请求,这样每个人都不会遇到问题。
,@Clifort 在他的回答中写的所有内容都是正确的,但是(使用 gcc)而不是 (void)
很难搜索,因为它还会显示我会使用的没有参数的函数
int aUnused __attribute__((unused));
当我看到代码时,它是不言自明的,而且很容易搜索。
(void) 有时会产生一些副作用,例如如果 (void)variable
为 variable
则为 volatile
生成死代码