在 GCC/G++ 中使用 -pedantic 的目的是什么?编译器?
此注释说:
-ansi
:告诉编译器实现ANSI语言选项。这转 关闭 GCC 的某些“功能” 与 ANSI 不兼容 标准。
-pedantic
< /a>:与-ansi
结合使用,告诉编译器严格遵守ANSI标准, 拒绝任何不符合的代码 合规。
首先要说的是:
- GCC/G++ 编译器的
-pedantic
和-ansi
选项的用途是什么(我无法理解上面的描述)? - 使用这两个选项的正确环境是什么?
- 我什么时候应该使用它们?
- 它们重要吗?
This note says:
-ansi
: tells the compiler to implement the ANSI language option. This turns
off certain "features" of GCC which
are incompatible with the ANSI
standard.
-pedantic
: used in conjunction with-ansi
, this tells the compiler to be adhere strictly to the ANSI standard,
rejecting any code which is not
compliant.
First things first:
- What is the purpose of the
-pedantic
and-ansi
options of the GCC/G++ compiler (I couldn't understand the above description)? - What are the right circumstances for using these two options?
- When should I use them?
- Are they important?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我在编码中一直使用它。
-ansi
标志相当于-std=c89
。如前所述,它关闭了 GCC 的一些扩展。添加-pedantic
会关闭更多扩展并生成更多警告。-pedantic
会发出警告,因为它超出了 C89 标准所需的最小限制。也就是说,每个 C89 编译器都必须接受长度为 509 的字符串;它们被允许接受更长的字符串,但是如果你很迂腐,那么使用更长的字符串是不可移植的,即使编译器被允许接受更长的字符串,并且在没有迂腐警告的情况下,GCC也会接受它们。I use it all the time in my coding.
The
-ansi
flag is equivalent to-std=c89
. As noted, it turns off some extensions of GCC. Adding-pedantic
turns off more extensions and generates more warnings.-pedantic
warns about that because it exceeds the minimum limit required by the C89 standard. That is, every C89 compiler must accept strings of length 509; they are permitted to accept longer, but if you are being pedantic, it is not portable to use longer strings, even though a compiler is permitted to accept longer strings and, without the pedantic warnings, GCC will accept them too.如果可能的话,GCC 编译器总是尝试编译您的程序。然而,在一些
在这种情况下,C 和 C++ 标准指定禁止某些扩展。符合标准的编译器
当遇到这些扩展时,例如 GCC 或 g++ 必须发出诊断。
例如,GCC 编译器的 -pedantic 选项会导致 GCC 在这种情况下发出警告。使用更严格的-pedantic-errors< /a> 选项将此类诊断警告转换为错误,从而导致编译在此类点上失败。只有那些需要由符合标准的编译器标记的非 ISO 构造才会生成警告或错误。
GCC compilers always try to compile your program if this is at all possible. However, in some
cases, the C and C++ standards specify that certain extensions are forbidden. Conforming compilers
such as GCC or g++ must issue a diagnostic when these extensions are encountered.
For example, the GCC compiler’s -pedantic option causes GCC to issue warnings in such cases. Using the stricter -pedantic-errors option converts such diagnostic warnings into errors that will cause compilation to fail at such points. Only those non-ISO constructs that are required to be flagged by a conforming compiler will generate warnings or errors.
-ansi
是一个过时的开关,要求编译器根据 30 年前的过时的 C 标准修订版、ISO/IEC 9899:1990< 进行编译/strong>,本质上是对ANSI标准X3.159-1989《编程语言C》的重新命名。为什么过时了?因为ISO发布C90后,ISO就一直负责C的标准化,并且 C90 的任何技术勘误均已由 ISO 标准化,因此更倾向于使用-std=c90
,如果没有此开关,最新的 GCC C 编译器将更容易 使用。符合 ISO/IEC 9899:2011 或最新的 2018 修订版中的 C 语言标准化,
不幸的是,有一些懒惰的编译器供应商认为坚持使用较旧的过时标准修订版是可以接受的。标准机构甚至无法提供标准化文档。
使用该开关有助于确保代码可以在这些过时的编译器中编译。
-pedantic
是一个有趣的问题。在没有-pedantic
的情况下,即使请求特定标准,GCC 仍然会允许一些 C 标准中不可接受的扩展。例如,考虑该程序C11 草案 n1570 第 6.7.6.2p1 段说:
C 标准要求数组长度大于零;并且本段位于约束中;该标准规定了以下内容5.1.1.3p1:
但是,如果使用
gcc -c -std=c90 pedantic_test.c
编译程序,则不会生成警告。-pedantic
使编译器实际上遵守 C 标准;因此,现在它将生成一条诊断消息,按照标准的要求:因此,为了获得最大的可移植性,指定标准修订版是不够的,您还必须使用
-pedantic
(或-pedantic-错误
),以确保 GCC 实际上符合标准的字面意思。问题的最后一部分是关于将
-ansi
与 C++ 一起使用。 ANSI 从未对 C++ 语言进行标准化 - 只是从 ISO 中采用它,因此这就像“法国标准化英语”一样有意义。然而,GCC 似乎仍然接受 C++,尽管听起来很愚蠢。-ansi
is an obsolete switch that requests the compiler to compile according to the 30-year-old obsolete revision of C standard, ISO/IEC 9899:1990, which is essentially a rebranding of the ANSI standard X3.159-1989 "Programming Language C. Why obsolete? Because after C90 was published by ISO, ISO has been in charge of the C standardization, and any technical corrigenda to C90 have been standardized by ISO. Thus it is more apt to use the-std=c90
.Without this switch, the recent GCC C compilers will conform to the C language standardized in ISO/IEC 9899:2011, or the newest 2018 revision.
Unfortunately there are some lazy compiler vendors that believe it is acceptable to stick to an older obsolete standard revision, for which the standardization document is not even available from standard bodies.
Using the switch helps ensuring that the code should compile in these obsolete compilers.
The
-pedantic
is an interesting one. In absence of-pedantic
, even when a specific standard is requested, GCC will still allow some extensions that are not acceptable in the C standard. Consider for example the programThe C11 draft n1570 paragraph 6.7.6.2p1 says:
The C standard requires that the array length be greater than zero; and this paragraph is in the constraints; the standard says the following 5.1.1.3p1:
However, if you compile the program with
gcc -c -std=c90 pedantic_test.c
, no warning is produced.-pedantic
causes the compiler to actually comply to the C standard; so now it will produce a diagnostic message, as is required by the standard:Thus for maximal portability, specifying the standard revision is not enough, you must also use
-pedantic
(or-pedantic-errors
) to ensure that GCC actually does comply to the letter of the standard.The last part of the question was about using
-ansi
with C++. ANSI never standardized the C++ language - only adopting it from ISO, so this makes about as much sense as saying "English as standardized by France". However GCC still seems to accept it for C++, as stupid as it sounds.基本上,它将使您的代码在其他也实现 ANSI 标准的编译器下更容易编译,并且,如果您小心使用哪些库/API 调用,则可以在其他操作系统/平台下编译。
第一个关闭 GCC 的特定功能(-ansi)。
第二个会抱怨任何不符合标准的东西(不仅是 GCC 的特定功能,还有您的构造。)(-迂腐)。
Basically, it will make your code a lot easier to compile under other compilers which also implement the ANSI standard, and, if you are careful in which libraries/API calls you use, under other operating systems/platforms.
The first one turns off specific features of GCC (-ansi).
The second one will complain about anything at all that does not adhere to the standard (not only specific features of GCC, but your constructs too.) (-pedantic).
如果您的代码需要可移植,那么您可以测试它是否可以在没有任何 GCC 扩展或其他非标准功能的情况下进行编译。如果您的代码使用
-pedantic -ansi
进行编译,那么理论上它应该可以使用任何其他 ANSI 标准编译器进行编译。If your code needs to be portable then you can test that it compiles without any GCC extensions or other non-standard features. If your code compiles with
-pedantic -ansi
then in theory it should compile OK with any other ANSI standard compiler.如果您正在编写的代码预计将在各种平台上使用许多不同的编译器进行编译,那么您自己使用这些标志将有助于确保您不会生成只能在 GCC 下编译的代码。
If you're writing code that you envisage is going to be compiled on a wide variety of platforms, with a number of different compilers, then using these flags yourself will help to ensure you don't produce code that only compiles under GCC.
其他人已经回答得很充分了。我只想添加一些常见扩展的示例:
返回
void
的main
函数。这不是由标准定义的,这意味着它仅适用于某些编译器(包括 GCC),而不适用于其他编译器。顺便说一下,int main()
和int main(int, char**)
是标准定义的两个签名。另一个流行的扩展是能够在其他函数中声明和定义函数:
这是非标准的。如果您想要这种行为,请查看 C++11 lambda
Others have answered sufficiently. I would just like to add a few examples of frequent extensions:
The
main
function returningvoid
. This is not defined by the standard, meaning it will only work on some compilers (including GCC), but not on others. By the way,int main()
andint main(int, char**)
are the two signatures that the standard does define.Another popular extension is being able to declare and define functions inside other functions:
This is nonstandard. If you want this kind of behavior, check out C++11 lambdas
Pedantic 使得 GCC 编译器拒绝所有 GNU C 扩展,而不仅仅是那些使其与 ANSI 兼容的扩展。
Pedantic makes it so that the GCC compiler rejects all GNU C extensions, not just the ones that make it ANSI compatible.