Uncommon but useful GCC command line options (2)

Source: Internet
Author: User

Uncommon but useful GCC command line options (2)
GuideThe gcc compiler provides a list of almost countless command line options. Of course, no one has ever used or mastered all of its command line options, but there are some command line options that every gcc user should know-even if not required. Some of them are very common, others are not very common, but they are not necessarily useful.Review

In this series of articles, we focus on some of the gcc command line options that are not commonly used but are useful. In section 1, we have discussed several such command line options.

I don't know if you can recall that at the beginning of the first part of this series of tutorials, I briefly mentioned the-Wall option that developers usually use to generate warnings, it does not include any special warnings. If you do not understand these special warnings and do not know how to generate them, don't worry. I will explain in detail all the details about them in this article.

This article also covers gcc warning options related to floating point values and how to better manage them when the gcc command line option list is large.

Before proceeding, remember that all examples, commands, and commands in this tutorial have been tested on the Ubuntu 16.04 LTS operating system and gcc 5.4.0.

Warnings not included in the generate-Wall Option

Although 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 options

If 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.
"

Conclusion

In 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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.