Uncommon but useful GCC command line options (2)

Uncommon but useful GCC command line options (2)

2016-12-13 08:43 Favorites: 2    

The gcc compiler provides an almost endless list of command-line options. Of course, no one will ever use or be proficient in all of its command-line options, but there are some command-line options that every gcc user should know - if not necessarily. Some of them are commonly used and others are less commonly used, but being less commonly used does not mean that they are less useful.

In this series of articles, we focus on some uncommon but useful gcc command line options, several of which have been mentioned in the first section .

I don't know if you can recall that at the beginning of the first part of this tutorial series, I briefly mentioned the options that developers usually use to generate warnings, and did not -Wallcover some special warnings. If you don't know about these special warnings, and don't know how to generate them, don't worry, I will explain all the details about them in this article.

In addition, this article will also cover gcc warning options related to floating point values, and how to better manage them when the gcc command line option list becomes large.

Before proceeding, please keep in mind that all examples, commands and instructions in this tutorial have been tested on Ubuntu 16.04 LTS operating system and gcc 5.4.0.

Generate warnings not included with -Wall option

Although the gcc compiler -Walloptions cover most warning flags, there are still some warnings that cannot be generated. In order to generate them, use -Wextrathe option.

For example, the following code:

      
      
       
       
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {
  5. int i=0;
  6. /* ...
  7. some code here
  8. ...
  9. */
  10. if(i);
  11. return 1;
  12. return 0;
  13. }

I accidentally ifput an extra semicolon after the condition. Now, if you compile using the gcc command below, no warnings will be generated.

      
      
       
       
  1. gcc -Wall test.c -o test

But if you -Wextracompile with the option:

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

will generate a warning like the following:

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

As is clear from the warning above, -Wextrathe option internally enables -Wempty-bodythe option to detect suspicious code and generate a warning. Below are all the warning flags enabled by this option.

  • -Wclobbered
  • -Wempty-body
  • -Wignored-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(only enabled if the and -Wunusedor options are used)-Wall
  • -Wunused-but-set-parameter (只有和-Wunused -Wall` options are used)

If you want to know more about the flags mentioned above, please check the gcc manual .

-WextraAdditionally, the option generates warnings for the following situations :

  • A pointer is compared 0with an integer <, <=, >, or >=
  • (C++ only) An enumeration type and a non-enumeration type both appear in a conditional expression
  • (C++ only) ambiguous virtual base
  • (C++ only) Array subscripting of register types
  • (C++ only) Addressing a variable of register type
  • (C++ only) Base class not initialized in derived class's copy constructor

generate warnings on equality comparisons of floating point values

As you probably already know, floating-point values ​​cannot be compared for exact equality (if not, please read the FAQ related to comparison of floating-point values ). But if you do this by accident, will the gcc compiler issue errors or warnings? Let's test it out:

Here is a piece of ==code that uses the operator to compare floating-point values:

      
      
       
       
  1. #include<stdio.h>
  2. void compare(float x, float y)
  3. {
  4. if(x == y)
  5. {
  6. printf("\n EQUAL \n");
  7. }
  8. }
  9. int main(void)
  10. {
  11. compare(1.234, 1.56789);
  12. return 0;
  13. }

Use the following gcc command (with -Walland -Wextraoptions) to compile this code:

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

Unfortunately, the command above does not generate any warnings related to floating-point value comparisons. A quick look at the gcc manual, there is a dedicated -Wfloat-equaloption to use in this case.

Here is the command with this option:

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

Here is the output produced by this command:

      
      
       
       
  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 in the output above, -Wfloat-equalthe option forces the gcc compiler to generate a warning related to comparisons of floating point values.

Here is what the gcc manual says about this option:

The idea behind this is that, sometimes, it is convenient for the programmer to think of floating-point values ​​as real numbers of approximately infinite precision. If you do this, then you need to figure out, by profiling the code, or otherwise, the maximum or possible maximum error introduced by this calculation, and then allow this when comparing (and when producing output, but that's a different question) error. In particular, you shouldn't check for equality, but for possible range overlap between two values; this is done with relational operators, so the equality comparison may be wrong.

How to better manage gcc command line options

If the command-line option list becomes large and unmanageable in the gcc command you use, you can put it in a text file and pass the filename as an argument to the gcc command. After that, you have to use @filecommand line options.

For example, the following line is your gcc command:

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

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

      
      
       
       
  1. $ cat gcc-options&nbsp;
  2. -Wall -Wextra -Wfloat-equal

This way, your gcc commands will be much more concise and manageable:

      
      
       
       
  1. gcc @gcc-options test.c -o test

Here's what the gcc manual @filesays about :

Read command line options from a file. The read option is then inserted in @fileplace of the original option. If the file does not exist or cannot be read, then this option will be treated as text and will not be deleted.

Options in the file are separated by spaces. Options that contain whitespace characters can be enclosed in single or double quotes to complete the option. Any character (including backslash: '\') may be included in an option by prefixing it with a '\'. If the file itself contains additional @fileoptions, it will be processed recursively.

in conclusion

In this series of tutorials, we have explained 5 uncommon but useful gcc command line options: -Save-temps, -g, -Wextra, -Wfloat-equaland @file. Remember to take the time to practice using each option, and don't forget to browse the gcc manual for full details on them.

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


via: https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options-2/

The gcc compiler provides an almost endless list of command-line options. Of course, no one will ever use or be proficient in all of its command-line options, but there are some command-line options that every gcc user should know - if not necessarily. Some of them are commonly used and others are less commonly used, but being less commonly used does not mean that they are less useful.

In this series of articles, we focus on some uncommon but useful gcc command line options, several of which have been mentioned in the first section .

I don't know if you can recall that at the beginning of the first part of this tutorial series, I briefly mentioned the options that developers usually use to generate warnings, and did not -Wallcover some special warnings. If you don't know about these special warnings, and don't know how to generate them, don't worry, I will explain all the details about them in this article.

In addition, this article will also cover gcc warning options related to floating point values, and how to better manage them when the gcc command line option list becomes large.

Before proceeding, please keep in mind that all examples, commands and instructions in this tutorial have been tested on Ubuntu 16.04 LTS operating system and gcc 5.4.0.

Generate warnings not included with -Wall option

Although the gcc compiler -Walloptions cover most warning flags, there are still some warnings that cannot be generated. In order to generate them, use -Wextrathe option.

For example, the following code:

      
      
 
 
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {
  5. int i=0;
  6. /* ...
  7. some code here
  8. ...
  9. */
  10. if(i);
  11. return 1;
  12. return 0;
  13. }

I accidentally ifput an extra semicolon after the condition. Now, if you compile using the gcc command below, no warnings will be generated.

      
      
 
 
  1. gcc -Wall test.c -o test

But if you -Wextracompile with the option:

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

will generate a warning like the following:

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

As is clear from the warning above, -Wextrathe option internally enables -Wempty-bodythe option to detect suspicious code and generate a warning. Below are all the warning flags enabled by this option.

  • -Wclobbered
  • -Wempty-body
  • -Wignored-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(only enabled if the and -Wunusedor options are used)-Wall
  • -Wunused-but-set-parameter (只有和-Wunused -Wall` options are used)

If you want to know more about the flags mentioned above, please check the gcc manual .

-WextraAdditionally, the option generates warnings for the following situations :

  • A pointer is compared 0with an integer <, <=, >, or >=
  • (C++ only) An enumeration type and a non-enumeration type both appear in a conditional expression
  • (C++ only) ambiguous virtual base
  • (C++ only) Array subscripting of register types
  • (C++ only) Addressing a variable of register type
  • (C++ only) Base class not initialized in derived class's copy constructor

generate warnings on equality comparisons of floating point values

As you probably already know, floating-point values ​​cannot be compared for exact equality (if not, please read the FAQ related to comparison of floating-point values ). But if you do this by accident, will the gcc compiler issue errors or warnings? Let's test it out:

Here is a piece of ==code that uses the operator to compare floating-point values:

      
      
 
 
  1. #include<stdio.h>
  2. void compare(float x, float y)
  3. {
  4. if(x == y)
  5. {
  6. printf("\n EQUAL \n");
  7. }
  8. }
  9. int main(void)
  10. {
  11. compare(1.234, 1.56789);
  12. return 0;
  13. }

Use the following gcc command (with -Walland -Wextraoptions) to compile this code:

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

Unfortunately, the command above does not generate any warnings related to floating-point value comparisons. A quick look at the gcc manual, there is a dedicated -Wfloat-equaloption to use in this case.

Here is the command with this option:

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

Here is the output produced by this command:

      
      
 
 
  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 in the output above, -Wfloat-equalthe option forces the gcc compiler to generate a warning related to comparisons of floating point values.

Here is what the gcc manual says about this option:

The idea behind this is that, sometimes, it is convenient for the programmer to think of floating-point values ​​as real numbers of approximately infinite precision. If you do this, then you need to figure out, by profiling the code, or otherwise, the maximum or possible maximum error introduced by this calculation, and then allow this when comparing (and when producing output, but that's a different question) error. In particular, you shouldn't check for equality, but for possible range overlap between two values; this is done with relational operators, so the equality comparison may be wrong.

How to better manage gcc command line options

If the command-line option list becomes large and unmanageable in the gcc command you use, you can put it in a text file and pass the filename as an argument to the gcc command. After that, you have to use @filecommand line options.

For example, the following line is your gcc command:

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

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

      
      
 
 
  1. $ cat gcc-options&nbsp;
  2. -Wall -Wextra -Wfloat-equal

This way, your gcc commands will be much more concise and manageable:

      
      
 
 
  1. gcc @gcc-options test.c -o test

Here's what the gcc manual @filesays about :

Read command line options from a file. The read option is then inserted in @fileplace of the original option. If the file does not exist or cannot be read, then this option will be treated as text and will not be deleted.

Options in the file are separated by spaces. Options that contain whitespace characters can be enclosed in single or double quotes to complete the option. Any character (including backslash: '\') may be included in an option by prefixing it with a '\'. If the file itself contains additional @fileoptions, it will be processed recursively.

in conclusion

In this series of tutorials, we have explained 5 uncommon but useful gcc command line options: -Save-temps, -g, -Wextra, -Wfloat-equaland @file. Remember to take the time to practice using each option, and don't forget to browse the gcc manual for full details on them.

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


via: https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options-2/

Guess you like

Origin blog.csdn.net/sukeeeeeeeee/article/details/53885584