Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC does not emit a warning when compiling [duplicate]

Tags:

c

gcc

gnu

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.

like image 278
Spikatrix Avatar asked Jan 03 '15 13:01

Spikatrix


People also ask

How do I enable warnings in GCC?

The warning is made into an error by -pedantic-errors . Same as -Wimplicit-int and -Wimplicit-function-declaration . This warning is enabled by -Wall . -Wimplicit-fallthrough is the same as -Wimplicit-fallthrough=3 and -Wno-implicit-fallthrough is the same as -Wimplicit-fallthrough=0 .

How do I ignore GCC warnings?

To answer your question about disabling specific warnings in GCC, you can enable specific warnings in GCC with -Wxxxx and disable them with -Wno-xxxx. From the GCC Warning Options: You can request many specific warnings with options beginning -W , for example -Wimplicit to request warnings on implicit declarations.

Which GCC flag is used to enable all compiler warnings?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.

Which option of GCC inhibit all warning messages?

If -Wfatal-errors is also specified, then -Wfatal-errors takes precedence over this option. Inhibit all warning messages. Make all warnings into errors. Make the specified warning into an error.


1 Answers

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).

like image 76
ouah Avatar answered Sep 27 '22 23:09

ouah