我如何向 gcc 指定 C 中的 {} init 不应该编译?
How can I specify to gcc that {} init in C shouldn't compile?
gcc 使用 -std=gnu99
,编译以下代码:
void f()
{
struct X data = {};
// do something with data
}
这是有效的 C 吗?
这是 gnu 扩展吗?
如何告诉 gcc 不接受这种初始化?
我想确保与其他编译器的兼容性(例如 visual 2015)
不,空初始化程序不是标准 C。它是 gcc 扩展。 获取详细说明。
通过指定 -std=gnu99
,您允许使用 GNU 扩展。您可以通过指定 -std=cXX
选项强制编译器只允许符合标准的代码。
来自gcc online manual(强调我的)
-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’. When a base standard is specified, the compiler accepts all programs following that standard plus those using GNU extensions that do not contradict it. 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. 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. 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. For example -std=gnu90 -Wpedantic warns about C++ style ‘//’ comments, while -std=gnu99 -Wpedantic does not.
-pedantic
选项将导致在这种情况下显示警告,-Werror
将导致所有警告被视为错误。
例如:
x1.c: In function ‘f’:
x1.c:11:19: error: ISO C forbids empty initializer braces [-Werror=pedantic]
struct X data = {};
如果您想拒绝包含 GNU 特定扩展的代码,请使用 -std=c99 -pedantic-errors
(-pedantic
会针对非标准扩展发出诊断,但不一定会完全拒绝代码)。但是,如果您想保证符合 ISO,请注意这不是 100% 的解决方案。来自 gcc
手册页:
Some users try to use -pedantic
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.
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
. We don't have plans to support such a feature in the near future.
gcc 使用 -std=gnu99
,编译以下代码:
void f()
{
struct X data = {};
// do something with data
}
这是有效的 C 吗? 这是 gnu 扩展吗?
如何告诉 gcc 不接受这种初始化?
我想确保与其他编译器的兼容性(例如 visual 2015)
不,空初始化程序不是标准 C。它是 gcc 扩展。
通过指定 -std=gnu99
,您允许使用 GNU 扩展。您可以通过指定 -std=cXX
选项强制编译器只允许符合标准的代码。
来自gcc online manual(强调我的)
-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’. When a base standard is specified, the compiler accepts all programs following that standard plus those using GNU extensions that do not contradict it. 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. 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. 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. For example -std=gnu90 -Wpedantic warns about C++ style ‘//’ comments, while -std=gnu99 -Wpedantic does not.
-pedantic
选项将导致在这种情况下显示警告,-Werror
将导致所有警告被视为错误。
例如:
x1.c: In function ‘f’:
x1.c:11:19: error: ISO C forbids empty initializer braces [-Werror=pedantic]
struct X data = {};
如果您想拒绝包含 GNU 特定扩展的代码,请使用 -std=c99 -pedantic-errors
(-pedantic
会针对非标准扩展发出诊断,但不一定会完全拒绝代码)。但是,如果您想保证符合 ISO,请注意这不是 100% 的解决方案。来自 gcc
手册页:
Some users try to use
-pedantic
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.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
. We don't have plans to support such a feature in the near future.