在范围内没有原型的情况下定义的函数的编译器警告? [英] Compiler warning for function defined without prototype in scope?

查看:690
本文介绍了在范围内没有原型的情况下定义的函数的编译器警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[问题来自此答案的注释主题.]

众所周知,自C99以来,调用尚未声明的函数(最好使用适当的原型)是错误的.

As everyone knows, since C99 it's an error to call a function that hasn't been declared, preferably with a proper prototype.

但是,除此之外,如果我定义在范围内没有原型声明的函数(可能包含在调用者正在使用的同一头文件中),我希望编译器向我发出警告. (除非该函数是静态的,否则所有这些都是没有意义的.)

But, going beyond that, I want my compiler to warn me if I define a function without a prototype declaration in scope, presumably included out of the same header file that the callers are using. (Unless the function is static, in which case all of this is moot.)

原因应该很明显:如果标头中包含原型声明,并且所有调用方都包含该声明,但是该声明未包含在定义函数的文件中,并且该函数的实际定义与外部定义有所不同原型,那么代表调用者完成的所有原型检查都是毫无价值的,实际上适得其反.有一个明显的错误,但不能保证完全被捕获.

The reason should be obvious: If there's a prototype declaration in a header, and it's included by all the callers, but it's not included in the file where the function is defined, and if the function's actual definition somehow differs from the external prototype, then all the prototype checking done on behalf the callers is worthless, and in fact counterproductively wrong. There's a glaring error, but it's not guaranteed to be caught at all.

是否存在可以检查此内容的常见编译器?我用-Wall尝试了gcc和clang,但他们没有. (我可以想象Gimpel皮棉-如果仍然存在-可以做到这一点,但我没有副本.)

Are there common compilers which can check this? I tried both gcc and clang with -Wall, and they don't. (I would imagine Gimpel lint -- if it's still around -- would do this, but I don't have a copy.)

理想情况下,我希望它也坚持认为原型存在于单独的头文件中,但是那是一回事,所以我不必坚持. (此附加规定的原因是,某些程序员可能会受到假设警告消息的干扰,可能会尝试通过在包含定义的.c文件顶部键入一个外部原型来使其静音,从而再次失败.目的.)

Ideally, I'd like it to also insist that the prototype exist in a separate header file, but that's different kettle of fish, so I don't insist on it. (The reason for this additional stipulation would be that some programmers, harried by the hypothetical warning message, might try to silence it by typing in an external prototype at the top of the .c file containing the definition, which, again, would defeat the purpose.)

推荐答案

如果您需要同时适用于gcc和clang的选项,那么最好的选择可能是

If you need an option which works on both gcc and clang, your best bet is probably -Wmissing-prototypes. As indicated in the gcc documentation, this will trigger if a global function is defined and either:

  • 以前没有声明;或

  • There was no previous declaration; or

先前的声明没有原型.

如果先前的声明与定义包含在同一文件中,它不会发出抱怨;也就是说,不需要声明位于头文件中.

It does not complain if the previous declaration is contained in the same file as the definition; that is, it does not require that the declaration be in a header file.

必须显式启用此选项; -Wall-Wextra均未启用它.

This option must be enabled explicitly; it is neither enabled by -Wall nor by -Wextra.

不幸的是,gcc仅对C和目标C允许该选项;不适用于C ++(大概是因为C ++不允许使用非原型函数声明).对于gcc,另一种可能性是 -Wmissing-declarations .仅当没有以前的声明时才产生此警告;否则,将仅生成警告.没有报告没有原型的先前声明(即int foo();).但是它可以在C和C ++上运行.同样,必须显式启用警告选项.

Unfortunately, gcc only allows that option for C and Objective C; not for C++ (presumably because C++ does not allow non-prototyped function declarations). For gcc, another possibility would be -Wmissing-declarations. This warning is only produced if there was no previous declaration; a previous declaration with no prototype (i.e. int foo();) is not reported. But it works on both C and C++. Again, the warning option must be enabled explicitly.

Clang也有一个-Wmissing-declarations选项,但这意味着完全不同,并且会自动启用(即使没有-W选项).例如,此选项控制有关不声明任何对象(struct { int a; };)的空声明(int;),空typedef(typedef int;)和未加标签的组合的投诉. Gcc还会发出有关这些构造的警告,但是没有明显的选项可以启用或禁用这些警告.

Clang also has a -Wmissing-declarations option, but it means something completely different and it is enabled automatically (even if there are no -W options). For example, this option controls the complaints about empty declarations (int;), empty typedefs (typedef int;) and untagged composites which don't declare any object (struct { int a; };). Gcc also issues warnings about these constructs, but there is no obvious option to enable or disable these warnings.

这篇关于在范围内没有原型的情况下定义的函数的编译器警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆