How can I get prototype warnings from g ++?

I currently have a project that uses g ++ to compile code. I am in the process of cleaning up the code, and I would like to make sure that all functions have prototypes to ensure that things like const char * are properly managed. 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 anyone tell me:
1) Why is gcc invalid? Is this a bug in gcc?
2) Is there a way to enable this warning?

EDIT:

Here is an example of cutting and pasting:

 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++ -xc -Wmissing-prototypes -c foo.cpp 
+6
source share
3 answers

Have you tried -Wmissing-declarations? This seems to work for g ++ and detects an error case that you are describing. I'm not sure which version they added, but it works for me in 4.3.3.

+3
source

When compiling a file with a .cpp extension, it compiles by default as C ++ code. In C ++, the requirement for function declarations is a mandatory, stringent requirement. There is no point in creating the -Wmissing-prototypes option for C ++.

In other words, you cannot “enable this warning” in C ++ because the “missing prototype” is always an error in C ++.

PS As a note: the concept of a prototype is specific only to the C language. In C ++ there are no "prototypes".

In C, a function declaration may or may not be a prototype, so it is necessary that the additional term be different from others. In C ++, function declarations are always "prototypes" (from the point of view of C), so in C ++ there is simply no need for this additional member. In C ++ declarations, declarations are simply function declarations. It just says it all.

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

The goal of -Wmissing-prototypes is different. This option only works when calling a function that does not have a visible prototype at the call point. In C, this is legal, but if you want to receive a warning in this case, you use the -Wmissing-prototypes option. In C ++, calling a function that does not have a visible declaration ("prototype") at the point of call is always a direct error, so C ++ compilers do not need an option like -Wmissing-prototypes .

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

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

+10
source

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

Take for example the following declaration:

 void foo(); 
  • In C, foo can be called with any number and type of arguments.
  • In C ++, foo does not accept any arguments (a compilation error if any arguments are passed).
+7
source

All Articles