在旧版本的 gcc 上使用 `-Wno-implicit-fallthrough`

Using `-Wno-implicit-fallthrough` on older versions of gcc

我有一个在 gcc 4.8.4 下构建良好的项目。我尝试使用 gcc 7 构建,并注意到很多 -Wimplicit-fallthrough= 警告。 As far as I'm aware,这确实是在 gcc 版本 7 中添加的。我现在在构建时使用 -Wno-implicit-fallthrough 来抑制这些警告。回到旧版本的 gcc,保留 -Wno-implicit-fallthrough 编译器标志确实 不会 导致任何错误,即使我不相信 gcc 4 识别这个选项。怎么会?是否允许 -Wno- 选项不被识别?

来自man gcc

When an unrecognized warning option is requested (e.g., -Wunknown-warning), GCC emits a diagnostic stating that the option is not recognized. However, if the -Wno- form is used, the behavior is slightly different: no diagnostic is produced for -Wno-unknown-warning unless other diagnostics are being produced. This allows the use of new -Wno- options with old compilers, but if something goes wrong, the compiler warns that an unrecognized option is present.

总结一下:如果一切顺利,未知的 -Wno-* 开关将被忽略以保持兼容性。