From 90921b273f224b9e2c30bc2d95c6b72dbb6188e5 Mon Sep 17 00:00:00 2001 From: Lv Feng Date: Thu, 8 Dec 2016 09:16:55 +0800 Subject: [PATCH 1/6] Update 20161201 Uncommon but useful GCC command line options - part 2.md --- ...seful GCC command line options - part 2.md | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/sources/tech/20161201 Uncommon but useful GCC command line options - part 2.md b/sources/tech/20161201 Uncommon but useful GCC command line options - part 2.md index cd1e12c817..9ede8fea8d 100644 --- a/sources/tech/20161201 Uncommon but useful GCC command line options - part 2.md +++ b/sources/tech/20161201 Uncommon but useful GCC command line options - part 2.md @@ -1,24 +1,23 @@ -ucasFL translating -Uncommon but useful GCC command line options - part 2 +Uncommon but useful GCC command line options - part 2不常见但是很有用的 GCC 命令行选项 - (二) ============================================================ ### On this page - -1. [Enable warnings that aren't covered by -Wall][1] +### 本文导航 +1.  [Enable warnings that aren't covered by -Wall][1] +1.  [启用 Wall 选项不包括的警告][1] 2. [Enable warning for floating point values in equity comparisons][2] +2.  [通过公平比较启用浮点值警告][2] 3. [How to better manage gcc command line options][3] +3.  [如何更好的管理 gcc 命令行选项][3] 4. [Conclusion][4] +4.  [结论][4] The gcc compiler offers a seemingly never-ending list of command line options. Of course, no body uses or has expertise on all of them, but there are a select bunch that every gcc user should - if not must - know. While some of them are commonly used, others are a bit uncommon but no less useful. - -In this article series, we are focusing on some of those uncommon but useful gcc command line options, and have already covered a couple of them in the [part 1][5]. - -If you recall, in the beginning of the first part of this tutorial series, I briefly mentioned that the -Wall option that developers generally use for generating warnings doesn't cover some specific warnings. If you aren't aware of these warnings and have no idea on how to enable them, worry not, as we'll be explaining all that in detail in this article.  - -Aside from this, we'll also be covering a gcc warning option related to floating point variables, as well as how to better manage the gcc command line options if the list grows large.  - -But before we move ahead, please keep in mind that all the examples, command, and instructions mentioned in this tutorial have been tested on Ubuntu 16.04 LTS, and the gcc version that we've used is 5.4.0. - +gcc 编译器看起来提供了无数的命令行选项。当然,没有人使用或者精通它所有的命令行选项,但是有一系列被精心挑选出来的命令行选项是每一个 gcc 用户都应该知道的 - 即使不是必须知道。然而它们中的一些很常见,其他一些不太常见但并不意味着它们的用处没前者大。 +In this article series, we are focusing on some of those uncommon but useful gcc command line options, and have already covered a couple of them in the [part 1][5].在这个系列的文章中,我们集中于一些不常见但是很有用的 gcc 命令行选项,在[第一节][5]已经涉及到几个这样的命令行选项。 +If you recall, in the beginning of the first part of this tutorial series, I briefly mentioned that the -Wall option that developers generally use for generating warnings doesn't cover some specific warnings. If you aren't aware of these warnings and have no idea on how to enable them, worry not, as we'll be explaining all that in detail in this article. 不知道你是否能够回想起,在这个系列教程的第一部分的开始,我简要的提到了 - Wall 选项,开发者们通常使用它来生成警告,但不包括一些特殊的警告。如果你不认识这些特殊警告并且不知道如何启用它们,不用担心,因为我将在这篇文章中详细讲解它们所有的细节。 +Aside from this, we'll also be covering a gcc warning option related to floating point variables, as well as how to better manage the gcc command line options if the list grows large. 除此以外,这篇文章也将涉及与浮点值相关的 gcc 警告选项,以及在目录变得很大的时候如何更好的管理 gcc 命令行选项。 +But before we move ahead, please keep in mind that all the examples, command, and instructions mentioned in this tutorial have been tested on Ubuntu 16.04 LTS, and the gcc version that we've used is 5.4.0.但是在往前看之前,请记住,在这个教程中的所有例子,命令,指令都已经在 Ubuntu 16.04 LTS 中测试过,并且我使用的 gcc 版本是 5.4.0. ### Enable warnings that aren't covered by -Wall While the -Wall option of the gcc compiler covers most of the warning flags, there are some that remain disabled. To enable them, use the **-Wextra** option. From c061e8d14a2dc45c04cbc60234adf0872a34b562 Mon Sep 17 00:00:00 2001 From: Lv Feng Date: Fri, 9 Dec 2016 23:42:02 +0800 Subject: [PATCH 2/6] Update 20161201 Uncommon but useful GCC command line options - part 2.md --- ...seful GCC command line options - part 2.md | 117 +++++++++--------- 1 file changed, 61 insertions(+), 56 deletions(-) diff --git a/sources/tech/20161201 Uncommon but useful GCC command line options - part 2.md b/sources/tech/20161201 Uncommon but useful GCC command line options - part 2.md index 9ede8fea8d..48bf2db115 100644 --- a/sources/tech/20161201 Uncommon but useful GCC command line options - part 2.md +++ b/sources/tech/20161201 Uncommon but useful GCC command line options - part 2.md @@ -1,28 +1,27 @@ -Uncommon but useful GCC command line options - part 2不常见但是很有用的 GCC 命令行选项 - (二) +不常见但是很有用的 GCC 命令行选项 (二) ============================================================ -### On this page ### 本文导航 -1.  [Enable warnings that aren't covered by -Wall][1] -1.  [启用 Wall 选项不包括的警告][1] -2. [Enable warning for floating point values in equity comparisons][2] -2.  [通过公平比较启用浮点值警告][2] -3. [How to better manage gcc command line options][3] +1.  [生成 Wall 选项不包括的警告][1] +2.  [通过相等比较生成浮点值警告][2] 3.  [如何更好的管理 gcc 命令行选项][3] -4. [Conclusion][4] 4.  [结论][4] -The gcc compiler offers a seemingly never-ending list of command line options. Of course, no body uses or has expertise on all of them, but there are a select bunch that every gcc user should - if not must - know. While some of them are commonly used, others are a bit uncommon but no less useful. -gcc 编译器看起来提供了无数的命令行选项。当然,没有人使用或者精通它所有的命令行选项,但是有一系列被精心挑选出来的命令行选项是每一个 gcc 用户都应该知道的 - 即使不是必须知道。然而它们中的一些很常见,其他一些不太常见但并不意味着它们的用处没前者大。 -In this article series, we are focusing on some of those uncommon but useful gcc command line options, and have already covered a couple of them in the [part 1][5].在这个系列的文章中,我们集中于一些不常见但是很有用的 gcc 命令行选项,在[第一节][5]已经涉及到几个这样的命令行选项。 -If you recall, in the beginning of the first part of this tutorial series, I briefly mentioned that the -Wall option that developers generally use for generating warnings doesn't cover some specific warnings. If you aren't aware of these warnings and have no idea on how to enable them, worry not, as we'll be explaining all that in detail in this article. 不知道你是否能够回想起,在这个系列教程的第一部分的开始,我简要的提到了 - Wall 选项,开发者们通常使用它来生成警告,但不包括一些特殊的警告。如果你不认识这些特殊警告并且不知道如何启用它们,不用担心,因为我将在这篇文章中详细讲解它们所有的细节。 -Aside from this, we'll also be covering a gcc warning option related to floating point variables, as well as how to better manage the gcc command line options if the list grows large. 除此以外,这篇文章也将涉及与浮点值相关的 gcc 警告选项,以及在目录变得很大的时候如何更好的管理 gcc 命令行选项。 -But before we move ahead, please keep in mind that all the examples, command, and instructions mentioned in this tutorial have been tested on Ubuntu 16.04 LTS, and the gcc version that we've used is 5.4.0.但是在往前看之前,请记住,在这个教程中的所有例子,命令,指令都已经在 Ubuntu 16.04 LTS 中测试过,并且我使用的 gcc 版本是 5.4.0. -### Enable warnings that aren't covered by -Wall +gcc 编译器提供了几乎无数的命令行选项列表。当然,没有人使用或者精通它所有的命令行选项,但是有一系列被精心挑选出来的命令行选项是每一个 gcc 用户都应该知道的 - 即使不是必须知道。然而它们中的一些很常见,其他一些不太常见,但不常见并不意味着它们的用处没前者大。 -While the -Wall option of the gcc compiler covers most of the warning flags, there are some that remain disabled. To enable them, use the **-Wextra** option. +在这个系列的文章中,我们集中于一些不常见但是很有用的 gcc 命令行选项,在[第一节][5]已经讲到几个这样的命令行选项。 -For example, take a look at the following code: +不知道你是否能够回想起,在这个系列教程的第一部分的开始,我简要的提到了 - Wall 选项,开发者们通常使用它来生成警告,但不包括一些特殊的警告。如果你不认识这些特殊警告并且不知道如何生成它们,不用担心,因为我将在这篇文章中详细讲解关于它们所有的细节。 + +除此以外,这篇文章也将涉及与浮点值相关的 gcc 警告选项,以及在目录变得很大的时候如何更好的管理 gcc 命令行选项。 + +但是在往前看之前,请记住,这个教程中的所有例子,命令,指令所使用的环境:基于 Ubuntu 16.04 LTS 操作系统, gcc 版本为 5.4.0. + +### 生成 -Wall 选项不包括的警告 + +尽管 gcc 编译器的 -Wall 选项涵盖了绝大多数警告标记,但依然有一些警告不能生成。为了生成它们,请使用 **-Wextra** 选项。 + +比如,看一下下面的代码: ``` #include @@ -41,15 +40,16 @@ int main() } ``` -I accidentally put a semicolon after the 'if' condition. Now, when the code was compiled using the following gcc command, no warning was produced. + +我意外的在 'if' 条件后面多打了一个分号。现在,当使用下面的 gcc 命令来进行编译,不会生成任何警告。 gcc -Wall test.c -o test -But when the -Wextra option was used: +但是如果同时使用 -Wextra 选项来进行编译: gcc -Wall -Wextra test.c -o test -A warning was produced: +会生成下面这样一个警告: ``` test.c: In function ‘main’: @@ -57,30 +57,25 @@ test.c:10:8: warning: suggest braces around empty body in an ‘if’ statement if(i); ``` -As clear from the warning shown above, the -Wextra option internally enabled the -Wempty-body flag, which in turned detected the suspicious code and issued the warning. Here is the complete list of warning flags enabled by this option: +正如上面的警告清楚展示的那样, -Wextra 选项从内部生成了 -Wempty-body 标记,从而检测可疑代码并生成警告。下面是列出了这个选项生成的全部警告标志。 +-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 选项同时使用时才会生成).  --Wclobbered, -Wempty-body, -Wignored-qualifiers, -Wmissing-field-initializers, -Wmissing-parameter-type (C only), -Wold-style-declaration (C only), -Woverride-init, -Wsign-compare, -Wtype-limits, -Wuninitialized, -Wunused-parameter (only with -Wunused or -Wall), and -Wunused-but-set-parameter (only with -Wunused or -Wall).  +如果你想对上面所提到的标志有更进一步的了解,请查看 [gcc 手册][6] -If you want to learn what the above mentioned flags do, head to [gcc's man page][6]. +继续往前看,遇到下面这些情况, -Wextra 选项也会生成警告: -Moving on, the -Wextra option also issues warnings in the following cases: +* 一个指针和整数 0 进行 <, <=, >, 或 >= 比较 +* (仅 C++)一个枚举类型和一个非枚举类型同时出现在一个条件表达中 +* (仅 C++)有歧义的虚拟基底 +* (仅 C++)寄存器类型的数组加下标 +* (仅 C++)对寄存器类型的变量进行取址 +* (仅 C++)基类没有在派生类复制构建函数中进行初始化 -* A pointer is compared against integer zero with <, <=, >, or >= -* (C++ only) An enumerator and a non-enumerator both appear in a - conditional expression. -* (C++ only) Ambiguous virtual bases. -* (C++ only) Subscripting an array that has been declared - register.  -* (C++ only) Taking the address of a variable that has been - declared register.  -* (C++ only) A base class is not initialized in a derived class's - copy constructor. +### 通过相等比较生成浮点值警告 -### Enable warning for floating point values in equity comparisons +你可能已经知道,浮点值不能进行确切的相等比较(如果不知道,请阅读与浮点值比较相关的 [FAQ][7])。但是如果你意外的这样做了, gcc 编译器是否会忽略这个错误或警告?让我们来测试一下: -As you might already know, one should never test for the exact equality of floating-point values (didn't know this - read the floating-point comparison-related FAQ [here][7]). But even if you accidentally do this, does the gcc compiler throw any warning or error? Let's check out: - -Here's a code that compares floating point variables using == operator: +下面是一段使用 == 运算符进行浮点值比较的代码: ``` #include @@ -101,71 +96,81 @@ int main(void) } ``` -And here's the gcc command (containing both -Wall and -Wextra options) used to compile this code: +使用下面的 gcc 命令(包含 -Wall 和 - Wextra 选项)来编译这段代码: gcc -Wall -Wextra test.c -o test -Sadly, the above command doesn't produce any warning related to floating point comparison. A quick look at GCC's man page reveals that there exists a dedicated option **-Wfloat-equal** that should be used in these scenarios. +悲剧的是,上面的命令没有生成任何与浮点值比较相关的警告。快速看一下 gcc 手册,有一个专用的 **-Wfloat-equal** 选项,在这种情形下应该使用。 -Here's the command containing this option: +下面是包含这个选项的命令: gcc -Wall -Wextra -Wfloat-equal test.c -o test -And following is the output it produced: +下面是这条命令产生的输出: ``` -test.c: In function ‘compare’: -test.c:5:10: warning: comparing floating point with == or != is unsafe [-Wfloat-equal] +test.c: 在函数 ‘compare’ 中: +test.c:5:10: 警告: 对浮点值进行 == 或 != 比较是不安全的 [-Wfloat-equal] if(x == y) ``` -So as you can see in the output above, the -Wfloat-equal option forced gcc to produce warning related to floating comparison. +正如上面你所看到的输出那样, -Wfloat-equal 选项会强制 gcc 编译器生成一个与浮点值比较相关的警告。 -Here is what [the gcc man page][8] says about this option: +这儿是[gcc 手册][8]关于这一选项的说明: ``` The idea behind this is that sometimes it is convenient (for the programmer) to consider floating-point values as approximations to infinitely precise real numbers. If you are doing this, then you need to compute (by analyzing the code, or in some other way) the maximum or likely maximum error that the computation introduces, and allow for it when performing comparisons (and when producing output, but that's a different problem). In particular, instead of testing for equality, you shouldcheck to see whether the two values have ranges that overlap; and this is done with the relational operators, so equality comparisons are probably mistaken. ``` +译文: +、、、这背后的想法是,有时,把浮点值考虑成近似无限精确的实数(对程序员来说)是方便的。如果你这样做,那么你需要通过分析代码,或者其他方式来计算所介绍的计算方式的最大值或可能的最大误差,然后允许它进行比较(并产生输出,但这是一个不同的问题)。特别地,你应该检查两者是否可能出现范围重叠;如果这时候使用关系运算符,相等比较就可能会出错。 +、、、 -### How to better manage gcc command line options +### 如何更好的管理 gcc 命令行选项 -If the list of command line options that you are using in your gcc command is becoming larger and difficult to manage, then you can put it in a text file, and pass that file's name as an argument to the gcc command. For this, you'll have to use the **@file**command line option. +如果在你使用的 gcc 命令中,命令行选项列表变得很大而且很难管理,那么你可以把它放在一个文本文件中,然后把文件名作为 gcc 命令的一个参数。之后,你必须使用 **@file** 命令行选项。 -For example, if following is your gcc command: +比如,下面这行是你的 gcc 命令: gcc -Wall -Wextra -Wfloat-equal test.c -o test -Then you can put the three warnings-related options in a file named, say 'gcc-options' : +然后你可以把这三个和警告相关的选项放到一个文件里,文件名叫做 'gcc-options': $ cat gcc-options  -Wall -Wextra -Wfloat-equal -And your gcc command becomes less cluttered and easy to manage: + +从而你的 gcc 命令会变得更加简洁并且易于管理: gcc @gcc-options test.c -o test -Here's what the gcc man page says about @file: +这儿是 gcc 手册关于 @file 的说明: ``` Read command-line options from file. The options read are inserted in place of the original @file option. If file does not exist, or cannot be read, then the option will be treated literally, and not removed. Options in file are separated by whitespace. A whitespace character may be included in an option by surrounding the entire option in either single or double quotes. Any character (including a backslash) may be included by prefixing the character to be included with a backslash. The file may itself contain additional @file options; any such options will be processed recursively. ``` +译文 +、、、 +从文件中读取命令行选项。读取到的选项随之被插入到原始 @file 选项所在的位置。如果文件不存在或者无法读取,那么这个选项就会被当成文字处理,而不会被删除。 +文件中的选项以空格分隔。空白字符可能被包含在一个由单引号或双引号所包围的完整选项中。任何字符(包括反斜杠: '\')均可能通过一个 '\' 前缀而包含在一个选项中。如果该文件本身包含额外的 @file 选项,那么它将会被递归处理。 +、、、 -### Conclusion +### 结论 -So we covered a total of 5 uncommon but useful gcc command line options in this tutorial series: -save-temps, -g, -Wextra, -Wfloat-equal, and @file. Do spend time practicing each of them and don't forget to go through all the details that the gcc man page offers about them.  +在这个系列的教程中,我们讲解了 5 个不常见但是很有用的 gcc 命令行选项: -Save-temps, -g, -Wextra, -Wfloat-equal 以及 @file。记得花时间练习使用每一个选项,同时不要忘了浏览 gcc 手册上面所提供的关于它们的全部细节。 + +你是否知道或使用其他像这样有用的 gcc 命令行选项,并希望把它们在全世界范围内分享?请在下面的评论区留下所有的细节。 -Do you know or use other such useful gcc command line options, and want to share them with the world? Leave all the details in comments below. -------------------------------------------------------------------------------- via: https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options-2/ 作者:[Ansh][a] -译者:[译者ID](https://github.com/译者ID) +译者:[ucasFL](https://github.com/ucasFL) 校对:[校对者ID](https://github.com/校对者ID) 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 From baab1ca27cabc77afad3f233d14fb1e647a4ed03 Mon Sep 17 00:00:00 2001 From: Lv Feng Date: Fri, 9 Dec 2016 23:44:00 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E7=BF=BB=E8=AF=91=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...seful GCC command line options - part 2.md | 186 ++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 translated/tech/20161201 Uncommon but useful GCC command line options - part 2.md diff --git a/translated/tech/20161201 Uncommon but useful GCC command line options - part 2.md b/translated/tech/20161201 Uncommon but useful GCC command line options - part 2.md new file mode 100644 index 0000000000..48bf2db115 --- /dev/null +++ b/translated/tech/20161201 Uncommon but useful GCC command line options - part 2.md @@ -0,0 +1,186 @@ +不常见但是很有用的 GCC 命令行选项 (二) +============================================================ + +### 本文导航 +1.  [生成 Wall 选项不包括的警告][1] +2.  [通过相等比较生成浮点值警告][2] +3.  [如何更好的管理 gcc 命令行选项][3] +4.  [结论][4] + +gcc 编译器提供了几乎无数的命令行选项列表。当然,没有人使用或者精通它所有的命令行选项,但是有一系列被精心挑选出来的命令行选项是每一个 gcc 用户都应该知道的 - 即使不是必须知道。然而它们中的一些很常见,其他一些不太常见,但不常见并不意味着它们的用处没前者大。 + +在这个系列的文章中,我们集中于一些不常见但是很有用的 gcc 命令行选项,在[第一节][5]已经讲到几个这样的命令行选项。 + +不知道你是否能够回想起,在这个系列教程的第一部分的开始,我简要的提到了 - Wall 选项,开发者们通常使用它来生成警告,但不包括一些特殊的警告。如果你不认识这些特殊警告并且不知道如何生成它们,不用担心,因为我将在这篇文章中详细讲解关于它们所有的细节。 + +除此以外,这篇文章也将涉及与浮点值相关的 gcc 警告选项,以及在目录变得很大的时候如何更好的管理 gcc 命令行选项。 + +但是在往前看之前,请记住,这个教程中的所有例子,命令,指令所使用的环境:基于 Ubuntu 16.04 LTS 操作系统, gcc 版本为 5.4.0. + +### 生成 -Wall 选项不包括的警告 + +尽管 gcc 编译器的 -Wall 选项涵盖了绝大多数警告标记,但依然有一些警告不能生成。为了生成它们,请使用 **-Wextra** 选项。 + +比如,看一下下面的代码: + +``` +#include +#include +int main() +{ + int i=0; + /* ... + some code here + ... + */ + + if(i); + return 1; + return 0; +} +``` + + +我意外的在 'if' 条件后面多打了一个分号。现在,当使用下面的 gcc 命令来进行编译,不会生成任何警告。 + +gcc -Wall test.c -o test + +但是如果同时使用 -Wextra 选项来进行编译: + +gcc -Wall -Wextra test.c -o test + +会生成下面这样一个警告: + +``` +test.c: In function ‘main’: +test.c:10:8: warning: suggest braces around empty body in an ‘if’ statement [-Wempty-body] + if(i); +``` + +正如上面的警告清楚展示的那样, -Wextra 选项从内部生成了 -Wempty-body 标记,从而检测可疑代码并生成警告。下面是列出了这个选项生成的全部警告标志。 +-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 选项同时使用时才会生成).  + +如果你想对上面所提到的标志有更进一步的了解,请查看 [gcc 手册][6] + +继续往前看,遇到下面这些情况, -Wextra 选项也会生成警告: + +* 一个指针和整数 0 进行 <, <=, >, 或 >= 比较 +* (仅 C++)一个枚举类型和一个非枚举类型同时出现在一个条件表达中 +* (仅 C++)有歧义的虚拟基底 +* (仅 C++)寄存器类型的数组加下标 +* (仅 C++)对寄存器类型的变量进行取址 +* (仅 C++)基类没有在派生类复制构建函数中进行初始化 + +### 通过相等比较生成浮点值警告 + +你可能已经知道,浮点值不能进行确切的相等比较(如果不知道,请阅读与浮点值比较相关的 [FAQ][7])。但是如果你意外的这样做了, gcc 编译器是否会忽略这个错误或警告?让我们来测试一下: + +下面是一段使用 == 运算符进行浮点值比较的代码: + +``` +#include + +void compare(float x, float y) +{ + if(x == y) + { + printf("\n EQUAL \n"); + } +} + +int main(void) +{ + compare(1.234, 1.56789); + + return 0; +} +``` + +使用下面的 gcc 命令(包含 -Wall 和 - Wextra 选项)来编译这段代码: + +gcc -Wall -Wextra test.c -o test + +悲剧的是,上面的命令没有生成任何与浮点值比较相关的警告。快速看一下 gcc 手册,有一个专用的 **-Wfloat-equal** 选项,在这种情形下应该使用。 + +下面是包含这个选项的命令: + +gcc -Wall -Wextra -Wfloat-equal test.c -o test + +下面是这条命令产生的输出: + +``` +test.c: 在函数 ‘compare’ 中: +test.c:5:10: 警告: 对浮点值进行 == 或 != 比较是不安全的 [-Wfloat-equal] + if(x == y) +``` + +正如上面你所看到的输出那样, -Wfloat-equal 选项会强制 gcc 编译器生成一个与浮点值比较相关的警告。 + +这儿是[gcc 手册][8]关于这一选项的说明: + +``` +The idea behind this is that sometimes it is convenient (for the programmer) to consider floating-point values as approximations to infinitely precise real numbers. If you are doing this, then you +need to compute (by analyzing the code, or in some other way) the maximum or likely maximum error that the computation introduces, and allow for it when performing comparisons (and when producing +output, but that's a different problem). In particular, instead of testing for equality, you shouldcheck to see whether the two values have ranges that overlap; and this is done with the relational operators, so equality comparisons are probably mistaken. +``` +译文: +、、、这背后的想法是,有时,把浮点值考虑成近似无限精确的实数(对程序员来说)是方便的。如果你这样做,那么你需要通过分析代码,或者其他方式来计算所介绍的计算方式的最大值或可能的最大误差,然后允许它进行比较(并产生输出,但这是一个不同的问题)。特别地,你应该检查两者是否可能出现范围重叠;如果这时候使用关系运算符,相等比较就可能会出错。 +、、、 + +### 如何更好的管理 gcc 命令行选项 + +如果在你使用的 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 的说明: + +``` +Read command-line options from file. The options read are inserted in place of the original @file option. If file does not exist, or cannot be read, then the option will be treated literally, and not removed. + +Options in file are separated by whitespace. A whitespace character may be included in an option by surrounding the entire option in either single or double quotes. Any character (including a backslash) may be included by prefixing the character to be included with a backslash. The file may itself contain additional @file options; any such options will be processed recursively. +``` +译文 +、、、 +从文件中读取命令行选项。读取到的选项随之被插入到原始 @file 选项所在的位置。如果文件不存在或者无法读取,那么这个选项就会被当成文字处理,而不会被删除。 +文件中的选项以空格分隔。空白字符可能被包含在一个由单引号或双引号所包围的完整选项中。任何字符(包括反斜杠: '\')均可能通过一个 '\' 前缀而包含在一个选项中。如果该文件本身包含额外的 @file 选项,那么它将会被递归处理。 +、、、 + +### 结论 + +在这个系列的教程中,我们讲解了 5 个不常见但是很有用的 gcc 命令行选项: -Save-temps, -g, -Wextra, -Wfloat-equal 以及 @file。记得花时间练习使用每一个选项,同时不要忘了浏览 gcc 手册上面所提供的关于它们的全部细节。 + +你是否知道或使用其他像这样有用的 gcc 命令行选项,并希望把它们在全世界范围内分享?请在下面的评论区留下所有的细节。 + + +-------------------------------------------------------------------------------- + +via: https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options-2/ + +作者:[Ansh][a] +译者:[ucasFL](https://github.com/ucasFL) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]:https://twitter.com/howtoforgecom +[1]:https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options-2/#enable-warnings-that-arent-covered-by-wall +[2]:https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options-2/#enable-warning-fornbspfloating-point-values-in-equity-comparisons +[3]:https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options-2/#how-to-better-manage-gcc-command-line-options +[4]:https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options-2/#conclusion +[5]:https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options/ +[6]:https://linux.die.net/man/1/gcc +[7]:https://isocpp.org/wiki/faq/newbie +[8]:https://linux.die.net/man/1/gcc From 1b9c211da3e408fb5e3a4d051416e083b83393a2 Mon Sep 17 00:00:00 2001 From: Lv Feng Date: Fri, 9 Dec 2016 23:44:40 +0800 Subject: [PATCH 4/6] Delete 20161201 Uncommon but useful GCC command line options - part 2.md --- ...seful GCC command line options - part 2.md | 186 ------------------ 1 file changed, 186 deletions(-) delete mode 100644 sources/tech/20161201 Uncommon but useful GCC command line options - part 2.md diff --git a/sources/tech/20161201 Uncommon but useful GCC command line options - part 2.md b/sources/tech/20161201 Uncommon but useful GCC command line options - part 2.md deleted file mode 100644 index 48bf2db115..0000000000 --- a/sources/tech/20161201 Uncommon but useful GCC command line options - part 2.md +++ /dev/null @@ -1,186 +0,0 @@ -不常见但是很有用的 GCC 命令行选项 (二) -============================================================ - -### 本文导航 -1.  [生成 Wall 选项不包括的警告][1] -2.  [通过相等比较生成浮点值警告][2] -3.  [如何更好的管理 gcc 命令行选项][3] -4.  [结论][4] - -gcc 编译器提供了几乎无数的命令行选项列表。当然,没有人使用或者精通它所有的命令行选项,但是有一系列被精心挑选出来的命令行选项是每一个 gcc 用户都应该知道的 - 即使不是必须知道。然而它们中的一些很常见,其他一些不太常见,但不常见并不意味着它们的用处没前者大。 - -在这个系列的文章中,我们集中于一些不常见但是很有用的 gcc 命令行选项,在[第一节][5]已经讲到几个这样的命令行选项。 - -不知道你是否能够回想起,在这个系列教程的第一部分的开始,我简要的提到了 - Wall 选项,开发者们通常使用它来生成警告,但不包括一些特殊的警告。如果你不认识这些特殊警告并且不知道如何生成它们,不用担心,因为我将在这篇文章中详细讲解关于它们所有的细节。 - -除此以外,这篇文章也将涉及与浮点值相关的 gcc 警告选项,以及在目录变得很大的时候如何更好的管理 gcc 命令行选项。 - -但是在往前看之前,请记住,这个教程中的所有例子,命令,指令所使用的环境:基于 Ubuntu 16.04 LTS 操作系统, gcc 版本为 5.4.0. - -### 生成 -Wall 选项不包括的警告 - -尽管 gcc 编译器的 -Wall 选项涵盖了绝大多数警告标记,但依然有一些警告不能生成。为了生成它们,请使用 **-Wextra** 选项。 - -比如,看一下下面的代码: - -``` -#include -#include -int main() -{ - int i=0; - /* ... - some code here - ... - */ - - if(i); - return 1; - return 0; -} -``` - - -我意外的在 'if' 条件后面多打了一个分号。现在,当使用下面的 gcc 命令来进行编译,不会生成任何警告。 - -gcc -Wall test.c -o test - -但是如果同时使用 -Wextra 选项来进行编译: - -gcc -Wall -Wextra test.c -o test - -会生成下面这样一个警告: - -``` -test.c: In function ‘main’: -test.c:10:8: warning: suggest braces around empty body in an ‘if’ statement [-Wempty-body] - if(i); -``` - -正如上面的警告清楚展示的那样, -Wextra 选项从内部生成了 -Wempty-body 标记,从而检测可疑代码并生成警告。下面是列出了这个选项生成的全部警告标志。 --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 选项同时使用时才会生成).  - -如果你想对上面所提到的标志有更进一步的了解,请查看 [gcc 手册][6] - -继续往前看,遇到下面这些情况, -Wextra 选项也会生成警告: - -* 一个指针和整数 0 进行 <, <=, >, 或 >= 比较 -* (仅 C++)一个枚举类型和一个非枚举类型同时出现在一个条件表达中 -* (仅 C++)有歧义的虚拟基底 -* (仅 C++)寄存器类型的数组加下标 -* (仅 C++)对寄存器类型的变量进行取址 -* (仅 C++)基类没有在派生类复制构建函数中进行初始化 - -### 通过相等比较生成浮点值警告 - -你可能已经知道,浮点值不能进行确切的相等比较(如果不知道,请阅读与浮点值比较相关的 [FAQ][7])。但是如果你意外的这样做了, gcc 编译器是否会忽略这个错误或警告?让我们来测试一下: - -下面是一段使用 == 运算符进行浮点值比较的代码: - -``` -#include - -void compare(float x, float y) -{ - if(x == y) - { - printf("\n EQUAL \n"); - } -} - -int main(void) -{ - compare(1.234, 1.56789); - - return 0; -} -``` - -使用下面的 gcc 命令(包含 -Wall 和 - Wextra 选项)来编译这段代码: - -gcc -Wall -Wextra test.c -o test - -悲剧的是,上面的命令没有生成任何与浮点值比较相关的警告。快速看一下 gcc 手册,有一个专用的 **-Wfloat-equal** 选项,在这种情形下应该使用。 - -下面是包含这个选项的命令: - -gcc -Wall -Wextra -Wfloat-equal test.c -o test - -下面是这条命令产生的输出: - -``` -test.c: 在函数 ‘compare’ 中: -test.c:5:10: 警告: 对浮点值进行 == 或 != 比较是不安全的 [-Wfloat-equal] - if(x == y) -``` - -正如上面你所看到的输出那样, -Wfloat-equal 选项会强制 gcc 编译器生成一个与浮点值比较相关的警告。 - -这儿是[gcc 手册][8]关于这一选项的说明: - -``` -The idea behind this is that sometimes it is convenient (for the programmer) to consider floating-point values as approximations to infinitely precise real numbers. If you are doing this, then you -need to compute (by analyzing the code, or in some other way) the maximum or likely maximum error that the computation introduces, and allow for it when performing comparisons (and when producing -output, but that's a different problem). In particular, instead of testing for equality, you shouldcheck to see whether the two values have ranges that overlap; and this is done with the relational operators, so equality comparisons are probably mistaken. -``` -译文: -、、、这背后的想法是,有时,把浮点值考虑成近似无限精确的实数(对程序员来说)是方便的。如果你这样做,那么你需要通过分析代码,或者其他方式来计算所介绍的计算方式的最大值或可能的最大误差,然后允许它进行比较(并产生输出,但这是一个不同的问题)。特别地,你应该检查两者是否可能出现范围重叠;如果这时候使用关系运算符,相等比较就可能会出错。 -、、、 - -### 如何更好的管理 gcc 命令行选项 - -如果在你使用的 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 的说明: - -``` -Read command-line options from file. The options read are inserted in place of the original @file option. If file does not exist, or cannot be read, then the option will be treated literally, and not removed. - -Options in file are separated by whitespace. A whitespace character may be included in an option by surrounding the entire option in either single or double quotes. Any character (including a backslash) may be included by prefixing the character to be included with a backslash. The file may itself contain additional @file options; any such options will be processed recursively. -``` -译文 -、、、 -从文件中读取命令行选项。读取到的选项随之被插入到原始 @file 选项所在的位置。如果文件不存在或者无法读取,那么这个选项就会被当成文字处理,而不会被删除。 -文件中的选项以空格分隔。空白字符可能被包含在一个由单引号或双引号所包围的完整选项中。任何字符(包括反斜杠: '\')均可能通过一个 '\' 前缀而包含在一个选项中。如果该文件本身包含额外的 @file 选项,那么它将会被递归处理。 -、、、 - -### 结论 - -在这个系列的教程中,我们讲解了 5 个不常见但是很有用的 gcc 命令行选项: -Save-temps, -g, -Wextra, -Wfloat-equal 以及 @file。记得花时间练习使用每一个选项,同时不要忘了浏览 gcc 手册上面所提供的关于它们的全部细节。 - -你是否知道或使用其他像这样有用的 gcc 命令行选项,并希望把它们在全世界范围内分享?请在下面的评论区留下所有的细节。 - - --------------------------------------------------------------------------------- - -via: https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options-2/ - -作者:[Ansh][a] -译者:[ucasFL](https://github.com/ucasFL) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]:https://twitter.com/howtoforgecom -[1]:https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options-2/#enable-warnings-that-arent-covered-by-wall -[2]:https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options-2/#enable-warning-fornbspfloating-point-values-in-equity-comparisons -[3]:https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options-2/#how-to-better-manage-gcc-command-line-options -[4]:https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options-2/#conclusion -[5]:https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options/ -[6]:https://linux.die.net/man/1/gcc -[7]:https://isocpp.org/wiki/faq/newbie -[8]:https://linux.die.net/man/1/gcc From 22bb1d823e1b1196083a4377f3e7eedb3fef8550 Mon Sep 17 00:00:00 2001 From: Flynn Date: Fri, 23 Dec 2016 15:39:07 +0800 Subject: [PATCH 5/6] translated --- ...ates Automatically on Debian and Ubuntu.md | 85 ------------------- ...ates Automatically on Debian and Ubuntu.md | 83 ++++++++++++++++++ 2 files changed, 83 insertions(+), 85 deletions(-) delete mode 100644 sources/tech/20161105 How to Install Security Updates Automatically on Debian and Ubuntu.md create mode 100644 translated/tech/20161105 How to Install Security Updates Automatically on Debian and Ubuntu.md diff --git a/sources/tech/20161105 How to Install Security Updates Automatically on Debian and Ubuntu.md b/sources/tech/20161105 How to Install Security Updates Automatically on Debian and Ubuntu.md deleted file mode 100644 index d355fd618c..0000000000 --- a/sources/tech/20161105 How to Install Security Updates Automatically on Debian and Ubuntu.md +++ /dev/null @@ -1,85 +0,0 @@ -ucasFL translating -How to Install Security Updates Automatically on Debian and Ubuntu -============================================================ - -It has been said before -and I couldn’t agree more- that some of the best system administrators are those who seem (note the use of the word seem here) to be lazy all the time. - -While that may sound somewhat paradoxical, I bet it must be true in most cases – not because they are not doing the job they are supposed to be doing, but rather because they have automated most of it. - -One of the critical needs of a Linux system is to be kept up to date with the latest security patches available for the corresponding distribution. - -In this article we will explain how to set up your Debian and Ubuntu system to auto install (or update) essential security packages or patches automatically when needed. - -Other Linux distributions such as [CentOS/RHEL configured to install security updates automatically][1]. - -Needless to say, you will need superuser privileges in order to perform the tasks outlined in this article. - -### Configure Automatic Security Updates On Debian/Ubuntu - -To begin, install the following packages: - -``` -# aptitude update -y && aptitude install unattended-upgrades apt-listchanges -y -``` - -where apt-listchanges will report what has been changed during an upgrade. - -Next, open /etc/apt/apt.conf.d/50unattended-upgrades with your preferred text editor and add this line inside the Unattended-Upgrade::Origins-Pattern block: - -``` -Unattended-Upgrade::Mail "root"; -``` - -Finally, use the following command to create and populated the required configuration file (/etc/apt/apt.conf.d/20auto-upgrades) to activate the unattended updates: - -``` -# dpkg-reconfigure -plow unattended-upgrades -``` - -Choose `Yes` when prompted to install unattended upgrades: - -[ - ![Configure Unattended Security Updates on Debian](http://www.tecmint.com/wp-content/uploads/2016/11/Configure-Unattended-Security-Updates-on-Debian.png) -][2] - -Configure Unattended Security Updates on Debian - -then check that the following two lines have been added to /etc/apt/apt.conf.d/20auto-upgrades: - -``` -APT::Periodic::Update-Package-Lists "1"; -APT::Periodic::Unattended-Upgrade "1"; -``` - -And add this line to make reports verbose: - -``` -APT::Periodic::Verbose "2"; -``` - -Last, inspect /etc/apt/listchanges.conf to make sure notifications will be sent to root. - -[ - ![Notify Security Updates on Debian](http://www.tecmint.com/wp-content/uploads/2016/11/Notify-Security-Updates-on-Debian.png) -][3] - -Notify Security Updates on Debian - -In this post we have explained how to make sure your system is updated regularly with the latest security patches. Additionally, you learned how to set up notifications in order to keep yourself informed when patches are applied. - -Do you have any questions about this article? Feel free to drop us a note using the comment form below. We look forward to hearing from you. - --------------------------------------------------------------------------------- - -via: http://www.tecmint.com/auto-install-security-updates-on-debian-and-ubuntu - -作者:[Gabriel Cánepa][a] -译者:[译者ID](https://github.com/译者ID) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 组织编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]:http://www.tecmint.com/author/gacanepa/ -[1]:http://www.tecmint.com/auto-install-security-patches-updates-on-centos-rhel/ -[2]:http://www.tecmint.com/wp-content/uploads/2016/11/Configure-Unattended-Security-Updates-on-Debian.png -[3]:http://www.tecmint.com/wp-content/uploads/2016/11/Notify-Security-Updates-on-Debian.png diff --git a/translated/tech/20161105 How to Install Security Updates Automatically on Debian and Ubuntu.md b/translated/tech/20161105 How to Install Security Updates Automatically on Debian and Ubuntu.md new file mode 100644 index 0000000000..060974d5da --- /dev/null +++ b/translated/tech/20161105 How to Install Security Updates Automatically on Debian and Ubuntu.md @@ -0,0 +1,83 @@ +如何在 Debian 和 Ubuntu 系统上上自动安装安全更新 +============================================================ + +之前已经说过,一些最优秀的系统管理员看上去(注意这里使用的词是 seem(看上去))总是很“懒”的,这句话我再同意不过了。 + +虽然这句话听起来有点荒谬,但我敢打赌在大多数情况下它是对的-不是因为他们不去做他们原本应该做的事情,而是因为他们已经让系统自动去完成这样的事情了。 + +对于 Linux 系统来说,一个最关键的需求是为相应的 Linux 版本保持更新最新的安全补丁。 + +在这篇文章中,我们将讨论如何在 Debian 和 Ubuntu 系统上进行设置,从而实现自动安装或更新重要的安装包或补丁。 + +其他的 Linux 版本:[CentOS/RHEL 配置自动安装安全更新][1] + +不必多说,为了执行这篇文章中所讲到的任务,你需要有超级用户特权。 + +### 在 Debian/Ubuntu 上配置自动安全更新 + +首先,安装下面这些安装包: + +``` +# aptitude update -y && aptitude install unattended-upgrades apt-listchanges -y +``` + +apt-listchanges 将会通知你在升级过程中发生的改变 + +接下来,用你最喜欢的文本编辑器打开 /etc/apt/apt.conf.d/50unattended-upgrades,然后在 the Unattended-Upgrade::Origins-Pattern block: 中间加入下面这行内容: + +``` +Unattended-Upgrade::Mail "root"; +``` +最后,执行下面的命令来生成所需的配置文件(/etc/apt/apt.conf.d/20auto-upgrades),从而激活自动更新: + +``` +# dpkg-reconfigure -plow unattended-upgrades +``` + +当提示安装自动升级时,选择 'Yes': + +[ + ![在 Debian 上配置自动安装更新](http://www.tecmint.com/wp-content/uploads/2016/11/Configure-Unattended-Security-Updates-on-Debian.png) +][2] + +在 Debian 上配置自动安装更新 + +然后检查下面这两行是否已经加入到文件 /etc/apt/apt.conf.d/20auto-upgrades 中了: + +``` +APT::Periodic::Update-Package-Lists "1"; +APT::Periodic::Unattended-Upgrade "1"; +``` + +增加下面这行内容使通知更加详细: + +``` +APT::Periodic::Verbose "2"; +``` + +最后,检查 /etc/apt/listchanges.conf 来确保通知能被发送给 root 用户。 + +[ + ![Debian 系统上提示安全更新](http://www.tecmint.com/wp-content/uploads/2016/11/Notify-Security-Updates-on-Debian.png) +][3] + +在 Debian 系统上提示安全更新 + +在这篇文章中,我们讨论了如何确保你的系统定期更新最新的安全补丁。另外,你也学习了如何设置 notifications(提示),从而确保新的补丁被应用时你能够被通知到。 + +你有任何关于这篇文章的问题吗?你可以在下面的评论栏留下你的问题。我们期待收到你的回复。 + +-------------------------------------------------------------------------------- + +via: http://www.tecmint.com/auto-install-security-updates-on-debian-and-ubuntu + +作者:[Gabriel Cánepa][a] +译者:[ucasFL](https://github.com/ucasFL) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 组织编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]:http://www.tecmint.com/author/gacanepa/ +[1]:http://www.tecmint.com/auto-install-security-patches-updates-on-centos-rhel/ +[2]:http://www.tecmint.com/wp-content/uploads/2016/11/Configure-Unattended-Security-Updates-on-Debian.png +[3]:http://www.tecmint.com/wp-content/uploads/2016/11/Notify-Security-Updates-on-Debian.png From 02804568cd044f1c2a8d985ad3f8c19b32be1dac Mon Sep 17 00:00:00 2001 From: Lv Feng Date: Fri, 23 Dec 2016 15:52:16 +0800 Subject: [PATCH 6/6] Delete 20161201 Uncommon but useful GCC command line options - part 2.md --- ...seful GCC command line options - part 2.md | 186 ------------------ 1 file changed, 186 deletions(-) delete mode 100644 translated/tech/20161201 Uncommon but useful GCC command line options - part 2.md diff --git a/translated/tech/20161201 Uncommon but useful GCC command line options - part 2.md b/translated/tech/20161201 Uncommon but useful GCC command line options - part 2.md deleted file mode 100644 index 48bf2db115..0000000000 --- a/translated/tech/20161201 Uncommon but useful GCC command line options - part 2.md +++ /dev/null @@ -1,186 +0,0 @@ -不常见但是很有用的 GCC 命令行选项 (二) -============================================================ - -### 本文导航 -1.  [生成 Wall 选项不包括的警告][1] -2.  [通过相等比较生成浮点值警告][2] -3.  [如何更好的管理 gcc 命令行选项][3] -4.  [结论][4] - -gcc 编译器提供了几乎无数的命令行选项列表。当然,没有人使用或者精通它所有的命令行选项,但是有一系列被精心挑选出来的命令行选项是每一个 gcc 用户都应该知道的 - 即使不是必须知道。然而它们中的一些很常见,其他一些不太常见,但不常见并不意味着它们的用处没前者大。 - -在这个系列的文章中,我们集中于一些不常见但是很有用的 gcc 命令行选项,在[第一节][5]已经讲到几个这样的命令行选项。 - -不知道你是否能够回想起,在这个系列教程的第一部分的开始,我简要的提到了 - Wall 选项,开发者们通常使用它来生成警告,但不包括一些特殊的警告。如果你不认识这些特殊警告并且不知道如何生成它们,不用担心,因为我将在这篇文章中详细讲解关于它们所有的细节。 - -除此以外,这篇文章也将涉及与浮点值相关的 gcc 警告选项,以及在目录变得很大的时候如何更好的管理 gcc 命令行选项。 - -但是在往前看之前,请记住,这个教程中的所有例子,命令,指令所使用的环境:基于 Ubuntu 16.04 LTS 操作系统, gcc 版本为 5.4.0. - -### 生成 -Wall 选项不包括的警告 - -尽管 gcc 编译器的 -Wall 选项涵盖了绝大多数警告标记,但依然有一些警告不能生成。为了生成它们,请使用 **-Wextra** 选项。 - -比如,看一下下面的代码: - -``` -#include -#include -int main() -{ - int i=0; - /* ... - some code here - ... - */ - - if(i); - return 1; - return 0; -} -``` - - -我意外的在 'if' 条件后面多打了一个分号。现在,当使用下面的 gcc 命令来进行编译,不会生成任何警告。 - -gcc -Wall test.c -o test - -但是如果同时使用 -Wextra 选项来进行编译: - -gcc -Wall -Wextra test.c -o test - -会生成下面这样一个警告: - -``` -test.c: In function ‘main’: -test.c:10:8: warning: suggest braces around empty body in an ‘if’ statement [-Wempty-body] - if(i); -``` - -正如上面的警告清楚展示的那样, -Wextra 选项从内部生成了 -Wempty-body 标记,从而检测可疑代码并生成警告。下面是列出了这个选项生成的全部警告标志。 --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 选项同时使用时才会生成).  - -如果你想对上面所提到的标志有更进一步的了解,请查看 [gcc 手册][6] - -继续往前看,遇到下面这些情况, -Wextra 选项也会生成警告: - -* 一个指针和整数 0 进行 <, <=, >, 或 >= 比较 -* (仅 C++)一个枚举类型和一个非枚举类型同时出现在一个条件表达中 -* (仅 C++)有歧义的虚拟基底 -* (仅 C++)寄存器类型的数组加下标 -* (仅 C++)对寄存器类型的变量进行取址 -* (仅 C++)基类没有在派生类复制构建函数中进行初始化 - -### 通过相等比较生成浮点值警告 - -你可能已经知道,浮点值不能进行确切的相等比较(如果不知道,请阅读与浮点值比较相关的 [FAQ][7])。但是如果你意外的这样做了, gcc 编译器是否会忽略这个错误或警告?让我们来测试一下: - -下面是一段使用 == 运算符进行浮点值比较的代码: - -``` -#include - -void compare(float x, float y) -{ - if(x == y) - { - printf("\n EQUAL \n"); - } -} - -int main(void) -{ - compare(1.234, 1.56789); - - return 0; -} -``` - -使用下面的 gcc 命令(包含 -Wall 和 - Wextra 选项)来编译这段代码: - -gcc -Wall -Wextra test.c -o test - -悲剧的是,上面的命令没有生成任何与浮点值比较相关的警告。快速看一下 gcc 手册,有一个专用的 **-Wfloat-equal** 选项,在这种情形下应该使用。 - -下面是包含这个选项的命令: - -gcc -Wall -Wextra -Wfloat-equal test.c -o test - -下面是这条命令产生的输出: - -``` -test.c: 在函数 ‘compare’ 中: -test.c:5:10: 警告: 对浮点值进行 == 或 != 比较是不安全的 [-Wfloat-equal] - if(x == y) -``` - -正如上面你所看到的输出那样, -Wfloat-equal 选项会强制 gcc 编译器生成一个与浮点值比较相关的警告。 - -这儿是[gcc 手册][8]关于这一选项的说明: - -``` -The idea behind this is that sometimes it is convenient (for the programmer) to consider floating-point values as approximations to infinitely precise real numbers. If you are doing this, then you -need to compute (by analyzing the code, or in some other way) the maximum or likely maximum error that the computation introduces, and allow for it when performing comparisons (and when producing -output, but that's a different problem). In particular, instead of testing for equality, you shouldcheck to see whether the two values have ranges that overlap; and this is done with the relational operators, so equality comparisons are probably mistaken. -``` -译文: -、、、这背后的想法是,有时,把浮点值考虑成近似无限精确的实数(对程序员来说)是方便的。如果你这样做,那么你需要通过分析代码,或者其他方式来计算所介绍的计算方式的最大值或可能的最大误差,然后允许它进行比较(并产生输出,但这是一个不同的问题)。特别地,你应该检查两者是否可能出现范围重叠;如果这时候使用关系运算符,相等比较就可能会出错。 -、、、 - -### 如何更好的管理 gcc 命令行选项 - -如果在你使用的 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 的说明: - -``` -Read command-line options from file. The options read are inserted in place of the original @file option. If file does not exist, or cannot be read, then the option will be treated literally, and not removed. - -Options in file are separated by whitespace. A whitespace character may be included in an option by surrounding the entire option in either single or double quotes. Any character (including a backslash) may be included by prefixing the character to be included with a backslash. The file may itself contain additional @file options; any such options will be processed recursively. -``` -译文 -、、、 -从文件中读取命令行选项。读取到的选项随之被插入到原始 @file 选项所在的位置。如果文件不存在或者无法读取,那么这个选项就会被当成文字处理,而不会被删除。 -文件中的选项以空格分隔。空白字符可能被包含在一个由单引号或双引号所包围的完整选项中。任何字符(包括反斜杠: '\')均可能通过一个 '\' 前缀而包含在一个选项中。如果该文件本身包含额外的 @file 选项,那么它将会被递归处理。 -、、、 - -### 结论 - -在这个系列的教程中,我们讲解了 5 个不常见但是很有用的 gcc 命令行选项: -Save-temps, -g, -Wextra, -Wfloat-equal 以及 @file。记得花时间练习使用每一个选项,同时不要忘了浏览 gcc 手册上面所提供的关于它们的全部细节。 - -你是否知道或使用其他像这样有用的 gcc 命令行选项,并希望把它们在全世界范围内分享?请在下面的评论区留下所有的细节。 - - --------------------------------------------------------------------------------- - -via: https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options-2/ - -作者:[Ansh][a] -译者:[ucasFL](https://github.com/ucasFL) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]:https://twitter.com/howtoforgecom -[1]:https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options-2/#enable-warnings-that-arent-covered-by-wall -[2]:https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options-2/#enable-warning-fornbspfloating-point-values-in-equity-comparisons -[3]:https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options-2/#how-to-better-manage-gcc-command-line-options -[4]:https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options-2/#conclusion -[5]:https://www.howtoforge.com/tutorial/uncommon-but-useful-gcc-command-line-options/ -[6]:https://linux.die.net/man/1/gcc -[7]:https://isocpp.org/wiki/faq/newbie -[8]:https://linux.die.net/man/1/gcc