As a popular open source compiler, GCC (GNU Compiler Collection) has become one of the standard features for Linux software development. If you want to enter the world of Linux programming, mastering GCC commands is essential. Whether you are a beginner or a professional developer, GCC opens up endless possibilities for you.
The gcc compiler provides an almost endless list of command line options. Of course, no one will ever use or be proficient in all its command-line options, but there are some command-line options that every gcc user should know - if not be required to know. Some of them are commonly used and others are less commonly used, but just because they are less commonly used does not mean that they are less useful than the former.
In this series of articles, we focus on some uncommon but useful gcc command line options. Several such command line options 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 series of tutorials, I briefly mentioned the -Wall option that developers usually use to generate warnings, and does not include 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 to this, this article will also cover gcc warning options related to floating point values, and how to better manage the gcc command line option list when it becomes large.
Before continuing, please remember 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 when -Wall option is not included
Although the -Wall option of the gcc compiler covers most warning flags, there are still some warnings that cannot be generated. To generate them, use the -Wextra option.
For example, the following code:
#include #include int main() { int i=0; /* ... some code here ... */ if(i); return 1; return 0; }
I accidentally put an extra semicolon after the if condition. Now, if you compile using the following gcc command, no warnings will be generated.
gcc -Wall test.c -o test
But if you use the -Wextra option at the same time to compile: gcc -Wall -Wextra test.c -o test
A warning like the following will be generated:
test.c: In function ‘main’: test.c:10:8: warning: suggest braces around empty body in an ‘if’ statement [-Wempty-body] if(i);
It is clear from the above warning that the -Wextra option enables the -Wempty-body option from the inside, so that suspicious code can be detected and warnings generated. Below are all the warning flags enabled by this option.
-Wclobbered -Wempty-body -Wignored-qualifiers -Wmissing-field-initializers -Wmissing-parameter-type(仅针对 C 语言) -Wold-style-declaration(仅针对 C 语言) -Woverride-init -Wsign-compare -Wtype-limits -Wuninitialized -Wunused-parameter(只有和-Wunused 或 -Wall 选项使用时才会启用) -Wunused-but-set-parameter (只有和-Wunused或-Wall 选项使用时才会生成)
If you want to know more about the tags mentioned above, please check the gcc manual.
In addition, the -Wextra option will also generate warnings when encountering the following situations:
一个指针和整数 0 进行 , 或 >= 比较 (仅 C++)一个枚举类型和一个非枚举类型同时出现在一个条件表达式中 (仅 C++)有歧义的虚拟基底 (仅 C++)寄存器类型的数组加下标 (仅 C++)对寄存器类型的变量进行取址 (仅 C++)基类没有在派生类的复制构建函数中进行初始化
Generate a warning when comparing floating point values for equality
You may already know that floating point values cannot be compared for exact equality (if not, please read the FAQ related to floating point value comparison). But if you accidentally do this, will the gcc compiler issue an error or warning? Let’s test it out:
The following is a piece of code that uses the == operator to compare floating point values:
#include 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 (including the -Wall and -Wextra options) to compile this code:
gcc -Wall -Wextra test.c -o test
Unfortunately, the above command does not generate any warnings related to floating point value comparisons. A quick look at the gcc manual shows that there is a dedicated -Wfloat-equal option that can be used in this situation.
Here is the command with this option:
gcc -Wall -Wextra -Wfloat-equal test.c -o test
The following 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, the -Wfloat-equal option forces the gcc compiler to generate a warning related to comparisons of floating point values.
Here is the gcc manual description of this option:
“
The idea behind this is that sometimes it is convenient for programmers to think of floating point values as approximately infinitely precise real numbers. 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 for this when doing the comparison (and when producing the output, but that's a different question) error. In particular, you should not check for equality, but you should check whether the two values may have overlapping ranges; this is done using relational operators, so the equality comparison may be a mistake.
”
How to better manage gcc command line options
如果在你使用的 gcc 命令中,命令行选项列表变得很大而且很难管理,那么你可以把它放在一个文本文件中,然后把文件名作为 gcc 命令的一个参数。之后,你必须使用@file 命令行选项。
比如,下面这行是你的 gcc 命令:
gcc -Wall -Wextra -Wfloat-equal test.c -o test
然后你可以把这三个和警告相关的选项放到一个文件里,文件名叫做 gcc-options:
$ cat gcc-options
-Wall -Wextra -Wfloat-equal
这样,你的 gcc 命令会变得更加简洁并且易于管理:
gcc @gcc-options test.c -o test
下面是 gcc 手册关于 @file 的说明:
“
从文件中读取命令行选项。读取到的选项随之被插入到原始 @file 选项所在的位置。如果文件不存在或者无法读取,那么这个选项就会被当成文字处理,而不会被删除。
文件中的选项以空格分隔。选项中包含空白字符的话,可以用一个由单引号或双引号包围完整选项。任何字符(包括反斜杠: ‘/’)均可能通过一个 ‘/’ 前缀而包含在一个选项中。如果该文件本身包含额外的 @file选项,那么它将会被递归处理。
”
结论
在这个系列的教程中,我们一共讲解了 5 个不常见但是很有用的 gcc 命令行选项:
-Save-temps -g -Wextra -Wfloat-equal @file
记得花时间练习使用每一个选项,同时不要忘了浏览 gcc 手册上面所提供的关于它们的全部细节。
因此,无论你是正在学习Linux编程,还是已经成为一名经验丰富的开发者,掌握GCC命令将会让你事半功倍。尝试使用GCC命令去编译各种不同的程序,挑战自己的极限,让你的Linux编程之路越来越精彩
The above is the detailed content of GCC Commands: Unlocking the Endless Possibilities of Linux Programming. For more information, please follow other related articles on the PHP Chinese website!