Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get missing prototype warnings from g++?

I currently have a project that uses g++ to compile it's code. I'm in the process of cleaning up the code, and I'd like to ensure that all functions have prototypes, to ensure things like const char * are correctly handled. Unfortunately, g++ complains when I try to specify -Wmissing-prototypes:

g++ -Wmissing-prototypes -Wall -Werror -c foo.cpp
cc1plus: warning: command line option "-Wmissing-prototypes" is valid for Ada/C/ObjC but not for C++

Can someone tell me:
1) Why does gcc this isn't valid? Is this a bug in gcc?
2) Is there a way to turn on this warning?

EDIT:

Here's a cut and paste example:

cat > foo.cpp <<EOF
void myfunc(int arg1, int arg2)
{
    /* do stuff with arg1, arg2 */
}
EOF
g++ -Wmissing-prototypes -c foo.cpp  # complains about not valid
g++ -c foo.cpp                       # no warnings
# Compile in C mode, warning appears as expected:
g++ -x c -Wmissing-prototypes -c foo.cpp
like image 930
Eric Avatar asked Mar 05 '10 18:03

Eric


3 Answers

When you compile a file with .cpp extension, it is compiled as C++ code by default. In C++ language the requirement for function declarations is a mandatory, hard requirement. There's no point in making an -Wmissing-prototypes option for C++.

In other words, you can't "turn on this warning" in C++ because "missing prototype" is always an error in C++.

P.S. As a side note: The notion of prototype is specific to C language only. There are no "prototypes" in C++.

In C language a function declaration can be a prototype or not a prototype, hence the need for an extra term to distinguish ones from the others. In C++ function declarations are always "prototypes" (from C point of view), so in C++ there simply no need for this extra term. In C++ function declarations are simply function declarations. That just says it all.

EDIT: After reading your comment I came to conclusion that you must have misunderstood the meaning and the purpose of the -Wmissing-prototypes option and corresponding warning. Note, this option will not check whether you have included prototypes of all your functions into some header file. There is no option to do that in GCC, regardless of whether you are using C or C++.

The purpose of -Wmissing-prototypes is different. This option only works when you call a function that has no visible prototype at the point of the call. In C language doing this is legal, but if you'd like a warning in this case, you use -Wmissing-prototypes option. In C++ language calling a function that has no visible declaration ("prototype") at the point of the call is always an immediate error, which is why C++ compilers have no need for such option as -Wmissing-prototypes.

In other words, if you defined some function in some implementation file, but forgot to include a prototype for this function in some header file, you will not get any warnings from the compiler until you actually try to call that function. It doesn't matter whether your code is C or C++, whether you use -Wmissing-prototypes or not... Until you make an attempt to call the function, there will be no warnings.

But once you try to call a function without a prototype, the C compiler will report a warning (if you used -Wmissing-prototypes) and C++ compiler will always report an error.

like image 137
AnT Avatar answered Oct 18 '22 00:10

AnT


-Wmissing-prototypes is not applicable for C++, because C++ always requires prototypes.

Take the following declaration for example:

void foo();
  • In C, foo can be called with any number and type of arguments.
  • In C++, foo does not take any arguments (compilation error if any arguments passed in).
like image 40
Chris Jester-Young Avatar answered Oct 18 '22 00:10

Chris Jester-Young


Did you try -Wmissing-declarations? That seems to work for g++ and detect the error case you describe. I'm not sure which version they added it in, but it works for me in 4.3.3.

like image 5
Steve Onorato Avatar answered Oct 18 '22 00:10

Steve Onorato