3

根据 GCC 手册,这些-Wreturn-type选项使用-Wall. 但是,我找不到在保持其余部分-Wall启用的同时禁用它的正确方法。

考虑这段代码:

func() {}

如果编译时没有警告,则不会产生任何输出。现在,如果我打开-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

好的,一切都很好。现在,如果使用 编译-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

因此,我得出结论,这-Wreturn-type对这些警告负责。但也许不是,或者我正在做更糟糕的事情,因为预计这不会产生任何输出:

$ gcc fn.c -Wall -Wno-return-type
fn.c:1: warning: return type defaults to ‘int’

是否可以使用-Wall但完全禁用-Wreturn-type?或者也许我错过了另一个可用的选项?

如果重要的话,我在 Mac OS 10.7(Darwin 11)和 GCC 4.2.1 上(奇怪的是,用 LLVM 编译:P)

4

1 回答 1

7

您可以使用-Wno-implicit-int禁用该警告:

gcc -c -Wall -Wno-return-type -Wno-implicit-int fn.c

但是,这可能会导致您可能希望看到的其他警告被禁用。例如,它将禁止对像这样声明的变量发出警告:

static x = 1;

选择你的毒药。

于 2011-08-29T23:50:24.913 回答