C++ 在不启用-Werror的情况下强制GCC4.x将-Wreturn类型视为错误?
假设我们有以下代码:C++ 在不启用-Werror的情况下强制GCC4.x将-Wreturn类型视为错误?,c++,gcc,return-value,gcc-warning,gcc4,C++,Gcc,Return Value,Gcc Warning,Gcc4,假设我们有以下代码: #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() {}
#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;
}
但如果/#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
(可能稍后会这样做,但这超出了这个问题的范围)
我的次级问题是:
#pragma
)- 将其命名为类似于
和gcc-wrapper
李>g++-wrapper
- 在Makefile中,将CC和CXX设置为包装器李>
- 让包装器调用GCC并将其输出传输到另一个程序,该程序将搜索所需的警告字符串
- 让搜索程序在发现警告时退出并显示错误
g++(GCC)4.4.3 20100127(Red Hat 4.4.3-4)谢谢Zan。实际上,我主要是想找到子问题1的答案。我不想从事携带包装器来破解我认为GCC本身应该支持的东西的业务。我开始得出结论,这是GCC本身的一个bug(或缺少的特性)。您是否找到了解决这个@bgoodr的解决方案。可能这实际上是编译器最新版本中修复的一个bug,但由于此时我没有升级编译器的选项,所以我将一直等到我们升级。
.../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