Uncommon but useful GCC command line options (2)
Compiled from: https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options-2/ Author: Ansh
Original: LCTT https://linux.cn/article-8032-1 .html Translator: ucasFL
Article address: https://linux.cn/article-8032-1.html
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 -Wall
cover 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 -Wall
options cover most warning flags, there are still some warnings that cannot be generated. In order to generate them, use -Wextra
the 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;
}
I accidentally if
put an extra semicolon after the condition. Now, if you compile using the gcc command below, no warnings will be generated.
gcc -Wall test.c -o test
But if you -Wextra
compile with the option:
gcc -Wall -Wextra test.c -o test
will generate a warning like the following:
test.c: In function ‘main’:
test.c:10:8: warning: suggest braces around empty body in an ‘if’ statement [-Wempty-body]
if(i);
As is clear from the warning above, -Wextra
the option internally enables -Wempty-body
the 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-Wunused
or 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 .
-Wextra
Additionally, the option generates warnings for the following situations :
- A pointer is compared
0
with 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:
#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;
}
Use the following gcc command (with -Wall
and -Wextra
options) to compile this code:
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-equal
option to use in this case.
Here is the command with this option:
gcc -Wall -Wextra -Wfloat-equal test.c -o test
Here is the output produced by 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 in the output above, -Wfloat-equal
the 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 @file
command line options.
For example, the following line 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
This way, your gcc commands will be much more concise and manageable:
gcc @gcc-options test.c -o test
Here's what the gcc manual @file
says about :
Read command line options from a file. The read option is then inserted in
@file
place 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
@file
options, 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-equal
and @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 -Wall
cover 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 -Wall
options cover most warning flags, there are still some warnings that cannot be generated. In order to generate them, use -Wextra
the 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;
}
I accidentally if
put an extra semicolon after the condition. Now, if you compile using the gcc command below, no warnings will be generated.
gcc -Wall test.c -o test
But if you -Wextra
compile with the option:
gcc -Wall -Wextra test.c -o test
will generate a warning like the following:
test.c: In function ‘main’:
test.c:10:8: warning: suggest braces around empty body in an ‘if’ statement [-Wempty-body]
if(i);
As is clear from the warning above, -Wextra
the option internally enables -Wempty-body
the 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-Wunused
or 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 .
-Wextra
Additionally, the option generates warnings for the following situations :
- A pointer is compared
0
with 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:
#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;
}
Use the following gcc command (with -Wall
and -Wextra
options) to compile this code:
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-equal
option to use in this case.
Here is the command with this option:
gcc -Wall -Wextra -Wfloat-equal test.c -o test
Here is the output produced by 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 in the output above, -Wfloat-equal
the 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 @file
command line options.
For example, the following line 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
This way, your gcc commands will be much more concise and manageable:
gcc @gcc-options test.c -o test
Here's what the gcc manual @file
says about :
Read command line options from a file. The read option is then inserted in
@file
place 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
@file
options, 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-equal
and @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/