强制 GCC 4.x 将 -Wreturn-type 视为错误而不启用 -Werror?
假设我们有以下代码:
#if !defined(__cplusplus)
# error This file should be compiled as C++
#endif
#include <stdio.h>
#include <string>
//#define USE_CXX_CLASS
#ifdef USE_CXX_CLASS
class SomeClass
{
public:
SomeClass() {}
~SomeClass() {}
std::string GetSomeString()
{
// case #1
}
};
#endif // USE_CXX_CLASS
int foo()
{
// case #2
}
int
main (int argc, char *argv[])
{
(void)argc;
(void)argv;
#ifdef USE_CXX_CLASS
SomeClass someInstance;
someInstance.GetSomeString();
#endif // USE_CXX_CLASS
foo();
return 0;
}
并假设它是从 GCC 版本 4.2.1 中使用选项 -Wreturn-type -Werror=return-type
。如果上面的代码按原样编译,而没有先取消注释上面的 //#define USE_CXX_CLASS
行,那么您将看到一条警告但没有错误:
.../gcc-4.2.1/bin/g++ -g -fPIC -Wreturn-type -Werror=return-type test.cpp -c -o test.o
test.cpp: In function 'int foo()':
test.cpp:26: warning: control reaches end of non-void function
但是如果 //#define USE_CXX_CLASS
行取消注释,则警告被视为错误:
.../gcc-4.2.1/bin/g++ -g -fPIC -Wreturn-type -Werror=return-type test.cpp -c -o test.o
test.cpp: In member function 'std::string SomeClass::GetSomeString()':
test.cpp:18: error: no return statement in function returning non-void [-Wreturn-type]
gmake: *** [test.o] Error 1
是的,一个是非成员函数(情况 #2),另一个是 C++函数(案例#1)。海事组织,这应该不重要。我希望这两个条件都被视为错误,并且我不想在此时添加 -Werror
或 -Wall
(可能稍后会这样做,但那超出了这个问题的范围)。
我的子问题是:
- 是否有一些我缺少的 GCC 开关应该可以工作? (不,我不想使用
#pragma
。) - 这是一个已在更新版本的 GCC 中解决的错误吗?
作为参考,我已经提出了其他类似的问题,包括以下内容:
Suppose we have the following code:
#if !defined(__cplusplus)
# error This file should be compiled as C++
#endif
#include <stdio.h>
#include <string>
//#define USE_CXX_CLASS
#ifdef USE_CXX_CLASS
class SomeClass
{
public:
SomeClass() {}
~SomeClass() {}
std::string GetSomeString()
{
// case #1
}
};
#endif // USE_CXX_CLASS
int foo()
{
// case #2
}
int
main (int argc, char *argv[])
{
(void)argc;
(void)argv;
#ifdef USE_CXX_CLASS
SomeClass someInstance;
someInstance.GetSomeString();
#endif // USE_CXX_CLASS
foo();
return 0;
}
And suppose that it were to be compiled the C++ compiler (and not the C compiler) from GCC version 4.2.1 with the options -Wreturn-type -Werror=return-type
. If the above code is compiled as is without first uncommenting the //#define USE_CXX_CLASS
line above, then you will see a warning but no error:
.../gcc-4.2.1/bin/g++ -g -fPIC -Wreturn-type -Werror=return-type test.cpp -c -o test.o
test.cpp: In function 'int foo()':
test.cpp:26: warning: control reaches end of non-void function
But if the //#define USE_CXX_CLASS
line is uncommented, then the warning is treated as an error:
.../gcc-4.2.1/bin/g++ -g -fPIC -Wreturn-type -Werror=return-type test.cpp -c -o test.o
test.cpp: In member function 'std::string SomeClass::GetSomeString()':
test.cpp:18: error: no return statement in function returning non-void [-Wreturn-type]
gmake: *** [test.o] Error 1
Yes, one is a non-member function (case #2), and the other is a C++ function (case #1). IMO, that should not matter. I want both conditions treated as an error, and I don't want to add -Werror
or -Wall
at this point in time (probably will do so later, but that is out of scope of this question).
My sub-questions are:
- Is there some GCC switch that I am missing that should work? (No I do not want to use
#pragma
's.) - Is this a bug that has been addressed in a more recent version of GCC?
For reference, I have already poured through other similar questions already, including the following:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它已被修复,它与
g++ 9.3
配合良好:成员函数和自由函数都被-Wall -Werror=return-type
视为错误It has been fixed, it works well with
g++ 9.3
: both member functions and free functions are treated as error with-Wall -Werror=return-type
即使没有 USE_CXX_CLASS 标志,我也确实看到了错误。即g++对于类成员函数和非成员函数的错误都是一致的。
g++ (GCC) 4.4.3 20100127(红帽 4.4.3-4)
I do see an error even w/o the USE_CXX_CLASS flag. i.e. g++ is consistent with the error for both class member functions and non member functions.
g++ (GCC) 4.4.3 20100127 (Red Hat 4.4.3-4)
在我看来,你需要的是一个围绕 gcc 的 shell 脚本包装器。
gcc-wrapper
和g++-wrapper
。It seems to me that what you need is a shell script wrapper around gcc.
gcc-wrapper
andg++-wrapper
.