我正在开发一个brew应用程序.在编译应用程序以获取MOD文件时,我不断收到此错误
cc1.exe:警告被视为错误
我想禁用此警告.我用google搜索过,很多人说禁用-werror会有所帮助,但我不知道该怎么做.编译器是CodeSourcery ARM.
我正进入(状态:
警告:假设假设(X + c)<X始终为假,则不会发生签名溢出[-Wstrict-overflow]
在这条线上:
if ( this->m_PositionIndex[in] < this->m_EndIndex[in] )
Run Code Online (Sandbox Code Playgroud)
m_PositionIndex和m_EndIndex类型itk::Index(http://www.itk.org/Doxygen/html/classitk_1_1Index.html),他们operator[]返回一个signed long.
(这是第37行:https://github.com/Kitware/ITK/blob/master/Modules/Core/Common/include/itkImageRegionConstIteratorWithIndex.hxx for context)
谁能解释一下这会引起什么警告?我没有看到(x+c) < x任何模式- 因为这只是一个signed long比较.
我尝试在一个自包含的示例中重现它:
#include <iostream>
namespace itk
{
struct Index
{
signed long data[2];
Index()
{
data[0] = 0;
data[1] = 0;
}
signed long& operator[](unsigned int i)
{
return data[i];
}
};
}
int main (int argc, char *argv[])
{
itk::Index positionIndex; …Run Code Online (Sandbox Code Playgroud) 我搜索了一下这方面的信息,但没有找到任何令人满意的.函数调用是否有一些特殊的行为
sprintf(someString, "");
Run Code Online (Sandbox Code Playgroud)
这解释了为什么这是警告(在gcc上使用-Wall)?我只是发现C标准允许零长度格式字符串.
我尝试了以下示例
#include <stdio.h>
int main()
{
char str[2] = {'a', 'a'};
sprintf(str, "");
printf("\'%c\'\'%c\'\n", str[0], str[1]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
打印出来的
'''a'
Run Code Online (Sandbox Code Playgroud)
这正是我期望看到的.那么,为什么警告?
较新版本的gcc提供了Wimplicit-fallthrough,这对于大多数switch语句都很有用.但是,我有一个switch语句,我希望允许所有case语句的掉头.
有没有办法明确地通过?我宁愿避免Wno-implicit-fallthrough为这个文件编译.
编辑:我正在寻找一种方法来通过显式(如果可能)使下降,而不是通过编译器开关或编译指示关闭警告.
我有一个在.h中定义的结构
struct buf_stats {
// ***
};
Run Code Online (Sandbox Code Playgroud)
然后在.c文件中
struct buf_stats *bs = malloc(sizeof(struct buf_states*)) ;
Run Code Online (Sandbox Code Playgroud)
哪里buf_states是拼写错误.
但gcc并没有警告我,虽然我用过 -Wall
这个错误/错字花了我3个小时才发现.
如何让gcc像这样警告未定义的结构?
我今天(2022 年 5 月 12 日)升级了整个arch Linux系统。也从 升级为。我尝试通过以下命令使用(编译器集合的一部分)编译我的一些程序:gccv11.2v12.1g++gcc
g++ -O3 -DNDEBUG -Os -Ofast -Og -s -march=native -flto -funroll-all-loops -std=c++20 main.cc -o ./main
Run Code Online (Sandbox Code Playgroud)
程序完美编译并正常运行,没有任何错误,但我收到警告:
lto-wrapper: warning: using serial compilation of 2 LTRANS jobs
Run Code Online (Sandbox Code Playgroud)
但是,当使用它编译同一程序时,v11.2它会产生零个错误和警告。
我的问题:
gcc版本升级所致v12.1这是g++我机器上的配置:
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit …Run Code Online (Sandbox Code Playgroud) 所以我有一些像这样的代码:
void foo (int, int);
void bar ( )
{
//Do Stuff
#if (IMPORTANT == 1)
foo (1, 2);
#endif
}
Run Code Online (Sandbox Code Playgroud)
在没有"重要"的情况下进行编译时,我得到一个编译器警告foo已定义且从未被引用.这让我思考(这就是问题).
所以为了解决这个问题,我只是#if (IMPORTANT == 1)在功能定义等地方添加了相同内容...以删除警告,然后我开始怀疑是否有不同的方法来抑制该功能的警告.我正在查看"未使用的"GCC attrib并且不知道函数是否具有我可以设置的相同属性?是否还有另一种方法可以抑制它只抑制那个函数而不是文件的警告?
我gcc使用以下方法编译以下示例-Wall -pedantic:
#include <stdio.h>
int main(void)
{
printf("main: %p\n", main); /* line 5 */
printf("main: %p\n", (void*) main); /* line 6 */
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我明白了:
main.c:5: warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘int (*)()’
main.c:6: warning: ISO C forbids conversion of function pointer to object pointer type
Run Code Online (Sandbox Code Playgroud)
第5行让我更改了第6行中的代码.
在打印功能的地址时删除警告我错过了什么?
初始化一个数组(在C++中,但任何适用于C的解决方案也可能在这里工作),初始化程序少于元素,这是完全合法的:
int array[10] = { 1, 2, 3 };
Run Code Online (Sandbox Code Playgroud)
但是,这可能是一些模糊的错误.有没有办法让编译器(gcc)检查一个特定数组的初始值设定项的数量,如果声明的实际大小不匹配,则发出警告甚至错误?
我知道我可以使用int array[] = { 1, 2, 3 };,然后可以使用静态断言sizeof(array)来验证我的期望.但我array在其他翻译单元中使用,所以我必须用明确的大小声明它.所以这个技巧对我不起作用.
虽然我知道这是有效的C不能区分枚举类型.
GCC确实-Wenum-compare(我正在使用)并按预期工作.
我试过使用,-Wconversion但这没有任何区别.
分配和算术运算符(+/ -/ &/ |...等)如何产生警告?(作业,或者......等)
{
enum Foo f = SOME_VALUE;
enum Bar b = SOME_OTHER_VALUE;
if (f != b) {
/* this warns! */
}
f = b; /* <-- how to warn about this? */
f |= b; /* .. and this? */
}
Run Code Online (Sandbox Code Playgroud)
笔记: