Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

warning not treated as error with -Wall & -Werror on

Here is the content of source file get.c :

#include <stdio.h>

int main(){
  //int i = 0;
  char b[10];
  gets(b);
  puts(b);
  return 0;
}

When I compile it with these command

gcc -o get get.c -Wall -Werror

The output is

/tmp/ccYEWZvx.o: In function `main':
get.c:(.text+0x10): warning: the `gets' function is dangerous and should not be used.

But when change the code to

#include <stdio.h>

int main(){
  int i = 0; // **this line just be uncommented**
  char b[10];
  gets(b);
  puts(b);
  return 0;
}

Using the same command, the output is

cc1: warnings being treated as errors  
get.c: In function 'main':  
get.c:4: error: unused variable 'i'  

So, why this unused variable warning be treated as error, while the use of gets() not?

like image 452
Yuan Wen Avatar asked Jun 26 '17 05:06

Yuan Wen


People also ask

How are errors not treated warnings?

You can make all warnings being treated as such using -Wno-error. You can make specific warnings being treated as such by using -Wno-error=<warning name> where <warning name> is the name of the warning you don't want treated as an error. If you want to entirely disable all warnings, use -w (not recommended).

Should I treat warnings as errors?

Yes, even once in a while you will encounter the occasional warning you'd be better off leaving as a warning or even disabling completely. Those should be the exception to the rule, though. Here's some practical advise: at the start of a new project, start treating all warnings as errors, by default.

Is warning an error?

WARNING: Something has not worked as it should. This may be of greater or lesser importance depending on the circumstances. e.g. An input file was not found, or was of the wrong format. ERROR: Something ``serious'' has gone wrong.


2 Answers

The gets() warning is being issued by the linker not the compiler, so the compiler settings do not apply.

Only the linker is able to determine that the symbol is resolved with the standard library gets() rather than some other implementation with the same name.

To instruct the linker to treat warnings as errors you need to pass it the --fatal-warnings option. In turn when not invoking the linker directly, options are passed to the linker by gcc using the -Wl option in a comma separated list:

gcc -o get get.c -Wall -Werror -Wl,--fatal-warnings

Note that the GNU linker is documented separately from the compiler, as part of binutils. The linker options are described here.

like image 183
Clifford Avatar answered Nov 15 '22 03:11

Clifford


If you look at the output from the first example, it says the "error" is in an object file, which means it is generated by the linker.

The second error is generated by the compiler, which means there is no object file being generated and so the linker will not be invoked.

like image 28
Some programmer dude Avatar answered Nov 15 '22 04:11

Some programmer dude