-Wpedantic 是否使程序遵循 -std=version?

Does -Wpedantic make the program follow the -std=version?

如果我使用 gcc -std=c17-Wpedantic 会发出针对 c17 的警告吗?这与 ISO C 相同吗?

根据gcc manual, "c17", "c18", "iso9899:2017" and "iso9899:2018" all designate the C standard that was published by ISO in 2018

根据same manual-Wpedantic取决于使用-std选项选择的标准版本:

Issue all the warnings demanded by strict ISO C and ISO C++; reject all programs that use forbidden extensions, and some other programs that do not follow ISO C and ISO C++. For ISO C, follows the version of the ISO C standard specified by any -std option used.

来自同一文档的重要说明:

Some users try to use -Wpedantic to check programs for strict ISO C conformance. They soon find that it does not do quite what they want: it finds some non-ISO practices, but not all—only those for which ISO C requires a diagnostic, and some others for which diagnostics have been added.

是的,一般来说 -std=cxx 选择要遵循的 C 标准,这与默认的 -std=gnuxx 设置有些相似,只是后者启用了各种语言扩展。基本上这个选项会选择可用的语言特性,但不会告诉编译器对其进行严格控制。

-Wpedantic-/-pedantic-errors 是使编译器严格的原因。 -std=c17 -Wpedantic 将把它变成一个严格(大部分)符合 ISO C 的编译器。它将从标准 headers 中删除各种 non-standard 扩展,并在您尝试各种 non-standard 功能时给出诊断消息。在某些情况下,即使在严格模式下,gcc 也无法符合标准,但大多数情况下,启用这些选项后它具有很好的一致性。

Clang 和 icc 编译器也使用相同的设置。