编译时GCC不会发出警告 [英] GCC does not emit a warning when compiling
问题描述
以下code编译和运行,但我希望警告编译时:
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;诠释主要(无效){ INT X = 10;
的printf(%P \\ N,&安培; X); 返回EXIT_SUCCESS;
}
-Wall -std = gnu99 -O2 -o a.out的source_file.c -pedantic -Wextra
在编译时给出了以下警告
source_file.c:在函数'主':
source_file.c:7:3:警告:格式'%P'需要类型'无效*'的说法,但参数2的类型'诠释*'[-Wformat =]
的printf(%P \\ N,&安培; X);
因为我没有添加一个(无效*)
在&放投; X
为%p
预计类型的参数无效*
。但当我编译使用
的gcc -o SO.c -Wall使-pedantic -Wextra -std = C11
或
的gcc -o SO.c -Wall使-pedantic -Wextra -std = C99
或
的gcc -o SO.c -Wall使-pedantic -Wextra -std = C89
GCC(在我的电脑)做的不可以发出警告,而使用编译(再一次在我的电脑)
的gcc -o SO.c -Wall使-pedantic -Wextra -std = gnu11
或
的gcc -o SO.c -Wall使-pedantic -Wextra -std = gnu99
或
的gcc -o SO.c -Wall使-pedantic -Wextra -std = gnu89
或
的gcc -o SO.c -Wall使-pedantic -Wextra
我收到上述警告。为什么会这样呢?我的版本的GCC 4.8.1是,我使用的是Windows。我从控制台,即, CMD
。
为什么会这样呢?
块引用>首先,我也可以重现我的机器上用的mingw32 GCC 4.8.1这种不一致的状态。
虽然诊断不需要(无约束冲突),由C标准,没有理由GCC问题诊断与
-std = gnu11
而不是-std = C11
。而且与我的机器上的Linux 4.8.2的gcc,诊断出现既
-std = C11
和-std = gnu11
。所以它看起来像一个bug在GCC(无论是在GCC 4.8.1或的mingw32 GCC 4.8.1)。
The following code compiles and runs but I expect a warning when compiling:
#include <stdio.h> #include <stdlib.h> int main(void){ int x = 10; printf("%p\n",&x); return EXIT_SUCCESS; }
GCC,from an online compiler with command line argument
-Wall -std=gnu99 -O2 -o a.out source_file.c -pedantic -Wextra
gives out the following warning when compiling
source_file.c: In function ‘main’: source_file.c:7:3: warning: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘int *’ [-Wformat=] printf("%p\n",&x);
because I've not added a
(void*)
cast before&x
as%p
expects an argument of typevoid*
.But when I compile usinggcc SO.c -o so -Wall -Wextra -pedantic -std=c11
or
gcc SO.c -o so -Wall -Wextra -pedantic -std=c99
or
gcc SO.c -o so -Wall -Wextra -pedantic -std=c89
GCC(in my PC) does not give out a warning whereas compiling(again in my PC) using
gcc SO.c -o so -Wall -Wextra -pedantic -std=gnu11
or
gcc SO.c -o so -Wall -Wextra -pedantic -std=gnu99
or
gcc SO.c -o so -Wall -Wextra -pedantic -std=gnu89
or
gcc SO.c -o so -Wall -Wextra -pedantic
I get the warning mentioned above. Why is it like that? My GCC version is 4.8.1 and I'm using Windows. I compile from the console,i.e,
cmd
.解决方案Why is it like that?
First, I can also reproduce this inconsistent state with mingw32 gcc 4.8.1 on my machine.
While the diagnostic is not required (no constraint violation) by the C Standard, there is no reason gcc issues the diagnostic with
-std=gnu11
and not with-std=c11
.Moreover with gcc 4.8.2 on Linux on my machine, the diagnostic appears with both
-std=c11
and-std=gnu11
.So it looks like a bug in gcc (either in gcc 4.8.1 or in mingw32 gcc 4.8.1).
这篇关于编译时GCC不会发出警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!