简体   繁体   English

如何指定不应该在C中编译的{} init来gcc?

[英]How can I specify to gcc that {} init in C shouldn't compile?

with gcc using -std=gnu99 , the following code compiles: 使用gcc使用-std=gnu99 ,将编译以下代码:

void f()
{
  struct X data = {};
  // do something with data
}

Is this valid C ? 这是有效的C吗? Is this a gnu extension ? 这是gnu扩展名吗?

How can I tell gcc to not accept this kind of init ? 我怎样才能告诉gcc不接受这种初始化?

I want to ensure compatibility with other compilers (like visual 2015 for example) 我想确保与其他编译器的兼容性(例如,Visual 2015)

No, the empty initializer is not standard C. It is a gcc extension. 不,空的初始化程序不是标准的C。它是gcc扩展。 See this for a detailed description. 有关详细说明, 请参见此。

By specifying -std=gnu99 , you allowed the GNU extensions to be used. 通过指定-std=gnu99 ,您允许使用GNU扩展。 You can force the compiler to only allow the standard conforming code, by specifying -std=cXX option. 您可以通过指定-std=cXX选项来强制编译器仅允许符合标准的代码。

From the gcc online manual ( emphasis mine ) gcc在线手册重点是我的

-std=

The compiler can accept several base standards, such as 'c90' or 'c++98', and GNU dialects of those standards, such as 'gnu90' or 'gnu++98'. 编译器可以接受多个基本标准,例如“ c90”或“ c ++ 98”,以及这些标准的GNU方言,例如“ gnu90”或“ gnu ++ 98”。 When a base standard is specified, the compiler accepts all programs following that standard plus those using GNU extensions that do not contradict it. 指定基本标准后,编译器将接受该标准之后的所有程序,以及使用与该标准不矛盾的GNU扩展的程序。 For example, -std=c90 turns off certain features of GCC that are incompatible with ISO C90, such as the asm and typeof keywords, but not other GNU extensions that do not have a meaning in ISO C90, such as omitting the middle term of a ?: expression. 例如,-std = c90关闭与ISO C90不兼容的GCC某些功能,例如asm和typeof关键字,但不关闭其他在ISO C90中没有意义的GNU扩展,例如省略中间的?:表达式。 On the other hand, when a GNU dialect of a standard is specified, all features supported by the compiler are enabled, even when those features change the meaning of the base standard. 另一方面, 当指定标准的GNU方言时,即使这些功能更改了基本标准的含义,也将启用编译器支持的所有功能。 As a result, some strict-conforming programs may be rejected. 结果,可能会拒绝某些严格符合要求的程序。 The particular standard is used by -Wpedantic to identify which features are GNU extensions given that version of the standard. 在给定标准版本的情况下,-Wpedantic使用特定的标准来标识哪些功能是GNU扩展。 For example -std=gnu90 -Wpedantic warns about C++ style '//' comments, while -std=gnu99 -Wpedantic does not. 例如,-std = gnu90 -Wpedantic会警告有关C ++样式的“ //”注释,而-std = gnu99 -Wpedantic不会。

The -pedantic option will cause a warning to be displayed in this case, and -Werror will cause all warnings to be treated as errors. 在这种情况下, -pedantic选项将导致显示警告,而-Werror将导致将所有警告视为错误。

For example: 例如:

x1.c: In function ‘f’:
x1.c:11:19: error: ISO C forbids empty initializer braces [-Werror=pedantic]
   struct X data = {};

If you want to reject code containing GNU-specific extensions, use -std=c99 -pedantic-errors ( -pedantic will issue diagnostics for non-standard extensions, but it won't necessarily reject the code outright). 如果要拒绝包含GNU特定扩展名的代码,请使用-std=c99 -pedantic-errors-pedantic将发出非标准扩展名的诊断信息,但不一定会完全拒绝该代码)。 However, if you want to guarantee ISO conformance, be aware that this isn't a 100% solution. 但是,如果要保证符合ISO标准,请注意,这不是100%的解决方案。 From the gcc man page: gcc手册页:

Some users try to use -pedantic to check programs for strict ISO C conformance. 一些用户尝试使用-pedantic检查程序是否严格符合-pedantic 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. 他们很快发现它并不能完全满足他们的要求:它找到了一些非ISO的实践,但并没有全部-仅是那些需要ISO C进行诊断的实践,以及一些已经添加了诊断的实践。

A feature to report any failure to conform to ISO C might be useful in some instances, but would require considerable additional work and would be quite different from -pedantic . 报告某些不符合ISO C要求的功能可能在某些情况下很有用,但将需要大量额外工作,并且与-pedantic会有很大不同。 We don't have plans to support such a feature in the near future. 我们没有计划在不久的将来支持这种功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM