Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

warnings when activating the optimization options

Tags:

c

linux

gcc

scanf

I use scanf in a c program to read an int from STDIN:

scanf("%d", &n);

when I compile the c program with optimization enabled I get some warnings:

gcc main.c -lm -lpthread -O2 -o main
main.c: In function ‘main’:
main.c:45: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
main.c:50: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result

but when I remove the optimization options, why don't I get those warnings?

gcc main.c -lm -lpthread -o main

P.S: I'm not using -Wall or something similar.

like image 451
ob_dev Avatar asked Jun 16 '12 08:06

ob_dev


People also ask

Which option can be used to display compiler warnings?

The warning message for each controllable warning includes the option that controls the warning. That option can then be used with -Werror= and -Wno-error= as described above. (Printing of the option in the warning message can be disabled using the -fno-diagnostics-show-option flag.)

What are optimization flags?

Turning on optimization flags makes the compiler attempt to improve the performance and/or code size at the expense of compilation time and possibly the ability to debug the program. The compiler performs optimization based on the knowledge it has of the program.

How do I turn off error warning?

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

How do I enable warnings in GCC?

GCC 4.3+ now has -Q --help=warnings , and you can even specify --help=warnings,C to just print out the C related warnings.


2 Answers

Changing optimizer settings changes how much (and how) the compiler analyzes your code.

Some program flow analysis is not done when optimization is not enabled (or not set high enough), so the related warnings are not issued.
You'll see that frequently for "unused variable" warnings - these require analysis of the code beyond what is necessary to simply compile it, so you'll ususally only get them with optimization enabled.

(And you really should be compiling with -Wall.)

like image 140
Mat Avatar answered Oct 02 '22 06:10

Mat


-Wunused-result is enabled by default: Because you'll actively need to decorate a function with __attribute__ ((warn_unused_result)) to trigger the warning, false positives only occur when it's used too liberally.

Even without passing additional flags, gcc should produce a warning. However, as Mat explained, the compiler doesn't do the necessary control flow analysis without increasing optimization levels.

Fix your code or silence the warning by adding -Wno-unused-result. Casting the return value to void will probably do as well.

To silence the warning in code, you'll need to assign the return value to a dummy variable, which you then can cast to void to avoid a new warning about the unused variable. It's also possible to substitute a C99 compound literal for the explicitly declared variable (tested with gcc 4.5.3).

This is indeed not optimal - I really expected the void-cast I originally porposed to work...

like image 24
Christoph Avatar answered Oct 02 '22 04:10

Christoph