编译时GCC不会发出警告 [英] GCC does not emit a warning when compiling

查看:230
本文介绍了编译时GCC不会发出警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code编译和运行,但我希望警告编译时:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;诠释主要(无效){  INT X = 10;
  的printf(%P \\ N,&安培; X);  返回EXIT_SUCCESS;
}

GCC,从命令行参数的在线编译器

  -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 type void* .But when I compile using

gcc 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆