Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is gcc's option "-Wstrict-prototypes" not valid for C++?

Here is a warning I, and lots of people out there on the web, see when running gcc on C++ code:

cc1plus: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++

The warning text is very clear: 'C++' is not in the set [Ada/C/ObjC ], so I have no question at all about why gcc gives this warning when compiling C++ code. (FYI the reason we have this flag turned on in spite of having C++ code is because it's mostly C code, we have chosen a strict (high level of) warning options list, but we've added a bit of C++ code.

My question is: Why isn't this warning valid for C++?

The gcc documentation for the warning option, from http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Warning-Options.html, is:

-Wstrict-prototypes (C and Objective-C only) Warn if a function is declared or defined without specifying the argument types. (An old-style function definition is permitted without a warning if preceded by a declaration which specifies the argument types.)

Now I just know I'm forgetting something obvious about C++, but doesn't C++ also require specifying argument types for functions in a prototype? True that those function prototypes are often in class declarations because the functions are often member functions, but aren't prototypes nevertheless required? Or even if they're just good practice, then why wouldn't gcc offer support by this option? Or if not, by a parallel option for C++?

like image 607
talkaboutquality Avatar asked Jul 14 '11 21:07

talkaboutquality


People also ask

How to provide GCC options through a file using @ option?

Provide gcc options through a file using @ option. The options to gcc can also be provided through a file. This can be done using the @ option followed by the file name containing the options. More than one options are separated by a white space.

How to produce only the assembly code using GCC?

The gcc command produces the output on stdout so you can redirect the output in any file. In our case (above), the file main.i would contain the preprocessed output. 4. Produce only the assembly code using -S option

How to use GCC compiler in Linux?

1. Specify the Output Executable Name. In its most basic form, gcc compiler can be used as : The above command executes the complete compilation process and outputs an executable with name a.out. Use option -o, as shown below, to specify the output file name for the executable. The command above would produce an output file with name ‘main’.

Is GCC free to use?

This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. vishal@vishal-HP-Notebook:~$ Show activity on this post. I get the same output when I call gcc without any file names, so it looks like gcc is working.


2 Answers

I suppose it's becuase C++ requires strict prototypes as part of the language, so the option is superfluous. Why that makes it so GCC needs to complain about it is beyond me.

I have that option set in my build script for small sample/test C or C++ programs, and the warning kind of irritates me - it seems like there's no reason to warn just because the default behavior for a language is what I'm asking for. But it's there, so one day when it irritates me enough I'll fix my script to not bother with that option for C++ builds.

like image 188
Michael Burr Avatar answered Oct 14 '22 16:10

Michael Burr


It's required by the C++ standard so there's no meaning of turning it on or off: It's always on in the language.

like image 40
Mark B Avatar answered Oct 14 '22 16:10

Mark B