Generate warnings not included with the - Wall option

Generate warnings not included with the - Wall option


Although the - Wall option of the gcc compiler covers most warning flags, some warnings cannot be generated. To generate them, use the - Wextra option.

For example, the following code:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i=0;
    /* ...
       some code here 
       ...
    */

    if(i);
        return 1;
     return 0; 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

I accidentally put an extra semicolon after the if condition. Now, if you compile using the following gcc command, no warning will be generated.

gcc -Wall test.c -o test
  • 1

However, if you also use the - Wextra option to compile:

gcc -Wall -Wextra test.c -o test
  • 1

The following warning is generated:

test.c: In function 'main':
test.c:10:8: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
 if(i);
  • 1
  • 2
  • 3

It is clear from the above warning that the - Wextra option enables the - wempty body option internally, so that suspicious codes can be detected and warnings can be generated. Here are all the warning flags enabled by this option.

-Wclobbered
-Wempty-body
-Wignored-qualifiers
-Wmissing-field-initializers
-Wmissing-parameter-type (Only for C (Language)
-Wold-style-declaration (Only for C (Language)
-Woverride-init
-Wsign-compare
-Wtype-limits
-Wuninitialized
-Wunused-parameter (Only and -Wunused or -Wall Option is enabled only when used)
-Wunused-but-set-parameter (Only and-Wunused or-Wall` (generated only when option is used)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

If you want to learn more about the tags mentioned above, please check the gcc manual.

In addition, the - Wextra option generates a warning when:

A pointer and an integer 0 <, <=, >, or >= compare
(only C++)An enumerated type and a non enumerated type appear in a conditional expression at the same time
(only C++)Ambiguous virtual base
(only C++)Subscript an array of register types
(only C++)Addressing variables of register type
(only C++)The base class is not initialized in the copy build function of the derived class
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Generates a warning when comparing floating-point values to their equivalents

You may already know that floating-point values cannot be compared for exact equality (if you don't know, please read the FAQ related to floating-point value comparison). But if you do this carelessly, will the gcc compiler report errors or warnings? Let's test:

The following is a code for floating-point value comparison using the = = operator:

#include<stdio.h>

void compare(float x, float y)
{
    if(x == y)
    {
        printf("\n EQUAL \n");
    }
}

int main(void)
{
    compare(1.234, 1.56789);

    return 0; 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

Compile this code using the following gcc command with the - Wall and - Wextra options:

gcc -Wall -Wextra test.c -o test

Unfortunately, the above command does not generate any warnings related to floating-point value comparisons. Take a quick look at the gcc manual, in which case you can use a dedicated - wfloat equal option.

The following command contains this option:

gcc -Wall -Wextra -Wfloat-equal test.c -o test

Here is the output from this command:

test.c: In function 'compare':
test.c:5:10: warning: comparing floating point with == or != is unsafe [-Wfloat-equal]
if(x == y)

As you can see from the output above, the - wfloat equal option forces the gcc compiler to generate a warning related to floating-point value comparison.

Here is a description of this option in the gcc Manual:

The idea behind this is that it is sometimes convenient for programmers to consider floating-point values as nearly infinitely accurate real numbers. If you do this, you need to analyze the code or other methods to calculate the maximum or possible maximum error introduced by this calculation method, Then, this error is allowed when comparing (and generating output, but this is a different problem). In particular, it should not be checked whether the two values are equal, but whether the ranges of the two values may overlap; this is done with relational operators, so the equal value comparison may be wrong.
  • 1

How to better manage gcc command line options

If the list of command line options in the gcc command you use becomes large and difficult to manage, you can put it in a text file and then use the file name as an argument to the gcc command. After that, you must use the @ file command line option.

For example, here is your gcc command:

gcc -Wall -Wextra -Wfloat-equal test.c -o test

Then you can put these three warning related options into a file called GCC options:

$ cat gcc-options
-Wall -Wextra -Wfloat-equal

In this way, your gcc commands will become more concise and easy to manage:

gcc @gcc-options test.c -o test

The following is the description of @ file in gcc Manual:

Read command line options from a file. The read options are then inserted into the original @file The location of the option. If the file does not exist or cannot be read, this option will be treated as word processing and will not be deleted.

The options in the file are separated by spaces. If an option contains white space characters, you can enclose the entire option in single or double quotation marks. Any character (including backslash): '')All possible through one '' Prefix is included in an option. If the file itself contains additional @file Option, it will be processed recursively.
  • 1
  • 2
  • 3

conclusion

In this series of tutorials, we explained five unusual but useful gcc command-line options: - save temps, - g, - Wextra, - wfloat equal, and @ file. Remember to take the time to practice using each option, and don't forget to browse all the details about them provided in the gcc manual.

Do you know or use other useful gcc command line options like this and want to share them around the world? Please leave all details in the comments area below.

Turn from https://www.zcfy.cc/article/uncommon-but-useful-gcc-command-line-options-part-2

Keywords: C GNU

Added by phpcip28 on Tue, 14 Dec 2021 10:17:16 +0200