c - GCC 在有效的 stb_image.h 上产生错误
问题描述
下面显示了 GCC 生成的两个错误:
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*,
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, 0, 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 最新
解决方案
代码初始化和修改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) ;
这将以一种没有实际效果并且可以优化的方式“使用”变量(尽管如果您选择使用这种对头文件的可怕滥用,显然这不是问题)。虚拟使用会抑制警告。
通常,您可能不想将标头更改为第三方库,但在这种情况下,标头就是库,因此如果说它是共享对象库的标头,则不存在不一致的风险。所有使用它的代码都可以看到更改。唯一的问题是,如果您采用更新版本,您可能必须重新实施修复。一种解决方案是向作者提交更改请求,这样每个人的问题都会消失。
推荐阅读
- interactive-brokers - Interactive Brokers TWS API 如何获取给定到期和行使价的期权合约?
- python-3.x - 根据其他列对 pandas 数据帧进行求和和规范化
- google-chrome - gmail帐户如何存储其他网站的密码
- javascript - messageReactionAdd 仅检测缓存的消息
- r - 如何执行点到栅格操作并在 R 中使用不同空间分辨率的栅格进行绘制?
- axios - axios 没有在任何请求中添加默认 baseUrl,即使它已设置
- apache-kafka-streams - kafka 流 - 加入分区主题
- excel - Excel获取列中的最后一个数据并从不同列返回值
- c# - 团结蹲伏
- html - 检查器窗口的 html 中的 ::marker 是什么意思?