Compiler warning for function defined without prototype in scope?

前端 未结 1 1974
北荒
北荒 2020-12-07 02:44

[Question inspired by a comment thread at this answer.]

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

相关标签:
1条回答
  • 2020-12-07 03:03

    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

    • The previous declaration had no prototype.

    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.

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

    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 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.

    0 讨论(0)
提交回复
热议问题