获取非原型声明的编译警告
在 C 中,函数声明可以是原型声明或非原型声明。例如,考虑以下最小程序:
int foo (); /* non-prototype declaration */
int bar (void); /* prototype declaration */
int main (int argc, char **argv)
{
return 0;
}
尽管在 C99 中非原型声明已过时,但我无法让 GCC 抱怨它们。例如,使用 GCC 编译上述程序并启用所有错误就成功了:
$ gcc -std=c99 -pedantic -Werror -Wall test.c
$
有什么方法可以说服 GCC 对不是原型的函数声明发出警告吗?
In C, function declarations can be prototype or non-prototype declarations. For example, consider the following minimal program:
int foo (); /* non-prototype declaration */
int bar (void); /* prototype declaration */
int main (int argc, char **argv)
{
return 0;
}
Although in C99 non-prototype declarations are obsolete, I cannot get GCC to complain about them. For example, compiling the above program with GCC and all errors enabled just succeeds:
$ gcc -std=c99 -pedantic -Werror -Wall test.c
$
Is there any way to persuade GCC to emit warnings for function declarations that aren't prototypes?
(Question inspired by an answer by Keith Thompson.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您正在寻找的选项是
-Wstrict-prototypes
I think the option you are looking for is
-Wstrict-prototypes
请注意,带有
-std=c99 -pedantic -Wall
选项的gcc
不会对旧式函数声明发出任何警告,但 C 不要求实现发出警告存在旧式函数声明时进行诊断。C 将旧式函数声明的使用描述为自 C89 以来已过时,但它仍然有效的 C 代码(在 C89/C99/C11 中)。
Note that
gcc
with-std=c99 -pedantic -Wall
options doesn't issue any warning for the old style function declarations but C doesn't require the implementation to issue a diagnostic in presence of the old style function declarations.C characterizes the use of old style function declaration as obsolescent since C89 but it it still valid C code (in C89/C99/C11).