Warnings not included in the generate-Wall OptionAlthough the-Wall option of the gcc compiler covers the vast majority of warning labels, there are still some warnings that cannot be generated. To generate them, use the-Wextra option.
For example, the following code:
1.#include
2.#include
3.int main()4.{5. int i=0;6. /* ...7. some code here 8. ...9. */10.11. if(i);12. return 1;13. return 0; 14.}
I accidentally typed a semicolon after the if condition. Now, if you use the following gcc command for compilation, no warning is generated.
1.gcc -Wall test.c -o test
However, if you use the-Wextra option for compilation at the same time:
1.gcc -Wall -Wextra test.c -o test
The following warning is generated:
1.test.c: In function ‘main’:2.test.c:10:8: warning: suggest braces around empty body in an ‘if’ statement [-Wempty-body]3. if(i);
From the preceding warning, the-Wextra option enables the-Wempty-body option internally to detect suspicious code and generate warnings. The following are all warning marks enabled for this option.
•-Wclobbered
•-Wempty-body
•-Wiggnored-qualifiers
•-Wmissing-field-initializers
•-Wmissing-parameter-type (for C language only)
•-Wold-style-declaration (for C language only)
•-Woverride-init
•-Wsign-compare
•-Wtype-limits
•-Wuninitialized
•-Wunused-parameter (enabled only when used with the-Wunused or-Wall option)
•-Wunused-but-set-parameter (generated only when used with the-Wunused or-Wall option)
If you want to learn more about the mark mentioned above, please refer to the gcc manual.
In addition, in the following cases, the-Wextra option also generates a warning:
• Compare a pointer with an integer 0 <, <=,>, or> =
• (C ++ only) One Enumeration type and one non-Enumeration type appear in a condition expression at the same time
• (C ++ only) ambiguous virtual bases
• (C ++ only) add subscript to the array of Register Type
• (C ++ only) addresses register-type variables
• (C ++ only) the base class is not initialized in the copy build function of the derived class
Generate a warning when comparing the values of floating point values.As you may already know, floating point values cannot be exactly equal (if you do not know, read the FAQ related to floating point value comparison ). But if you accidentally do this, will the gcc compiler report an error or warning? Let's test:
The following code compares floating point values using the = Operator:
1.#include
2.3.void compare(float x, float y)4.{5. if(x == y)6. {7. printf("\n EQUAL \n");8. }9.}10.11.int main(void)12.{13. compare(1.234, 1.56789);14.15. return 0; 16.}
Use the following gcc commands (including the-Wall and-Wextra options) to compile the Code:
1.gcc -Wall -Wextra test.c -o test
Unfortunately, the above command did not generate any warnings related to the comparison of floating point values. Take a quick look at the gcc manual. In this case, you can use a dedicated-Wfloat-equal option.
The following command contains this option:
1.gcc -Wall -Wextra -Wfloat-equal test.c -o test
The output of this command is as follows:
1.test.c: In function ‘compare’:2.test.c:5:10: warning: comparing floating point with == or != is unsafe [-Wfloat-equal]3. if(x == y)
As you can see above, the-Wfloat-equal option forces the gcc compiler to generate a warning related to the floating point value comparison.
Here is a description of this option in the gcc manual:
"The idea behind this is that sometimes it is convenient for programmers to consider a floating point value as an infinitely precise real number. 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, and then compare it (and when the output is generated, however, this is a different problem.) This error is allowed. In particular, we should not check whether the values are equal, but whether the two values may overlap the ranges. This is done using Relational operators, so the equivalence comparison may be wrong. "
How to better manage gcc command line optionsIf the command line option list becomes large and difficult to manage in your gcc command, you can put it in a text file, the file name is used as a parameter of the gcc command. Then, you must use the @ file command line option.
For example, the following line shows your gcc command:
1.gcc -Wall -Wextra -Wfloat-equal test.c -o test
Then you can put the three warning-related options in a file named gcc-options:
1.$ cat gcc-options2.-Wall -Wextra -Wfloat-equal
In this way, your gcc commands will become more concise and easy to manage:
1.gcc @gcc-options test.c -o test
The following is a description of @ file in the gcc manual:
"Read command line options from files. The read option is inserted to the location where the original @ file option is located. If the file does not exist or cannot be read, this option will be treated as text processing and will not be deleted.
File options are separated by spaces. If the option contains blank characters, you can use a complete option surrounded by single or double quotation marks. Any character (including backslash: '\') can be included in an option with a '\' prefix. If the file itself contains an additional @ file option, it will be recursively processed.
"
ConclusionIn this series of tutorials, we have explained a total of five uncommon but useful gcc command line options: -Save-temps,-g,-Wextra,-Wfloat-equal, and @ file. Remember to spend time practicing every option and never forget to read all the details about them in the gcc manual.
Do you know or use other useful gcc command line options like this and want to share them all over the world? Please leave all the details in the comment area below.
From: https://linux.cn/article-8032-1.html
Address: http://www.linuxprobe.com/usual-gcc-command.html