如何从g++中丢失原型警告?

时间:2021-01-26 15:05:27

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++来编译它的代码。我正在清理代码的过程中,我希望确保所有的函数都有原型,以确保像const char *这样的东西被正确处理。不幸的是,当我试图指定- wmistype原型时,g++会抱怨:

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?

谁能告诉我:1)为什么gcc这是无效的?这是gcc中的一个bug吗?有什么方法可以打开这个警告吗?

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

3 个解决方案

#1


3  

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.

你试过-Wmissing-declarations吗?这似乎适用于g++,并检测您描述的错误情况。我不确定他们添加了哪个版本,但对我来说是4.3.3。

#2


10  

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

当您用.cpp扩展编译一个文件时,默认情况下它被编译成c++代码。在c++语言中,对函数声明的要求是强制性的,硬性要求。在c++中做一个- wmisase -原型选项是没有意义的。

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

换句话说,在c++中,您不能“打开这个警告”,因为“丢失的原型”始终是c++中的一个错误。

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

附注:原型的概念仅限于C语言。在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.

在C语言中,函数声明可以是原型,也可以是原型,因此需要额外的术语来区分它们。在c++中,函数声明总是“原型”(从C的角度来看),所以在c++中,不需要这个额外的项。在c++中,函数声明是简单的函数声明。这就说明了一切。

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

编辑:在阅读你的评论后,我得出结论,你一定误解了- wmis原型选项的含义和目的,以及相应的警告。注意,这个选项不会检查您是否将所有函数的原型包含到某个头文件中。无论您使用的是C还是c++,在GCC中都没有选择。

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.

- wmisast -原型的目的是不同的。这个选项只有在调用一个没有可见原型的函数时才有效。在C语言中,这样做是合法的,但是如果你想在这种情况下得到警告,你可以使用- wmisase -原型选项。在c++语言中,调用一个在调用点上没有可见声明(“原型”)的函数总是一个立即的错误,这就是为什么c++编译器不需要这样的选项,比如- wmisingprototype。

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.

换句话说,如果您在某个实现文件中定义了某个函数,但是忘记在一些头文件中包含这个函数的原型,那么您将不会从编译器得到任何警告,直到您真正尝试调用该函数为止。不管你的代码是C还是c++,不管你使用的是- wmising原型还是…在您尝试调用该函数之前,没有警告。

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.

但是,当您尝试调用一个没有原型的函数时,C编译器会报告一个警告(如果您使用了- wmis原型),而c++编译器将总是报告一个错误。

#3


7  

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

- wmisingprototype不适用于c++,因为c++总是需要原型。

Take the following declaration for example:

以下列宣言为例:

void foo();
  • In C, foo can be called with any number and type of arguments.
  • 在C中,可以用任意数量和类型的参数来调用foo。
  • In C++, foo does not take any arguments (compilation error if any arguments passed in).
  • 在c++中,foo不接受任何参数(如果传入的参数是编译错误)。

#1


3  

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.

你试过-Wmissing-declarations吗?这似乎适用于g++,并检测您描述的错误情况。我不确定他们添加了哪个版本,但对我来说是4.3.3。

#2


10  

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

当您用.cpp扩展编译一个文件时,默认情况下它被编译成c++代码。在c++语言中,对函数声明的要求是强制性的,硬性要求。在c++中做一个- wmisase -原型选项是没有意义的。

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

换句话说,在c++中,您不能“打开这个警告”,因为“丢失的原型”始终是c++中的一个错误。

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

附注:原型的概念仅限于C语言。在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.

在C语言中,函数声明可以是原型,也可以是原型,因此需要额外的术语来区分它们。在c++中,函数声明总是“原型”(从C的角度来看),所以在c++中,不需要这个额外的项。在c++中,函数声明是简单的函数声明。这就说明了一切。

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

编辑:在阅读你的评论后,我得出结论,你一定误解了- wmis原型选项的含义和目的,以及相应的警告。注意,这个选项不会检查您是否将所有函数的原型包含到某个头文件中。无论您使用的是C还是c++,在GCC中都没有选择。

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.

- wmisast -原型的目的是不同的。这个选项只有在调用一个没有可见原型的函数时才有效。在C语言中,这样做是合法的,但是如果你想在这种情况下得到警告,你可以使用- wmisase -原型选项。在c++语言中,调用一个在调用点上没有可见声明(“原型”)的函数总是一个立即的错误,这就是为什么c++编译器不需要这样的选项,比如- wmisingprototype。

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.

换句话说,如果您在某个实现文件中定义了某个函数,但是忘记在一些头文件中包含这个函数的原型,那么您将不会从编译器得到任何警告,直到您真正尝试调用该函数为止。不管你的代码是C还是c++,不管你使用的是- wmising原型还是…在您尝试调用该函数之前,没有警告。

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.

但是,当您尝试调用一个没有原型的函数时,C编译器会报告一个警告(如果您使用了- wmis原型),而c++编译器将总是报告一个错误。

#3


7  

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

- wmisingprototype不适用于c++,因为c++总是需要原型。

Take the following declaration for example:

以下列宣言为例:

void foo();
  • In C, foo can be called with any number and type of arguments.
  • 在C中,可以用任意数量和类型的参数来调用foo。
  • In C++, foo does not take any arguments (compilation error if any arguments passed in).
  • 在c++中,foo不接受任何参数(如果传入的参数是编译错误)。