根据GCC手册,-Wreturn-type选项已启用-Wall.但是,我无法找到一种正确的方法来禁用它,同时保持其余的-Wall启用.
考虑以下代码:
func() {}
Run Code Online (Sandbox Code Playgroud)
如果在没有警告的情况下编译,则不会生成输出.现在,如果我打开,-Wall将出现以下行:
$ gcc fn.c -Wall
fn.c:1: warning: return type defaults to ‘int’
fn.c: In function ‘func’:
fn.c:1: warning: control reaches end of non-void function
Run Code Online (Sandbox Code Playgroud)
好的,一切都很好.现在,如果使用-Wreturn-type,则生成相同的输出:
$ gcc fn.c -Wreturn-type
fn.c:1: warning: return type defaults to ‘int’
fn.c: In function ‘func’:
fn.c:1: warning: control reaches end of non-void function
Run Code Online (Sandbox Code Playgroud)
所以,我的结论-Wreturn-type是负责这些警告.但也许不是,或者我做的更糟糕,因为预计不会产生任何输出:
$ gcc fn.c -Wall -Wno-return-type
fn.c:1: warning: return type defaults to ‘int’
Run Code Online (Sandbox Code Playgroud)
是否可以使用-Wall但-Wreturn-type完全禁用?或者也许我错过了另一个可用选项?
如果重要的话,我在Mac OS 10.7(Darwin 11)上使用GCC 4.2.1(奇怪的是,用LLVM编译:P)
您可以-Wno-implicit-int用来禁用该警告:
gcc -c -Wall -Wno-return-type -Wno-implicit-int fn.c
Run Code Online (Sandbox Code Playgroud)
但是,这可能会导致您可能希望禁用其他警告.例如,它将禁止对如此声明的变量发出警告:
static x = 1;
Run Code Online (Sandbox Code Playgroud)
选择你的毒药.