Compiler warning for function defined without prototype in scope?

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.

Leave a Comment