今天在一份源码中看到了 gcc -W -Wall ...,自己平时没有用过,于是查了下gcc手册。

查询说明

使用mac gcc查找三个选项的说明:

-w

-w Inhibit all warning messages.

从上面可以看出来-w是代表关闭编译时的所有警告,也就是不显示任何Warning

-Wall

-Wall
           This enables all the warnings about constructions that some users consider
           questionable, and that are easy to avoid (or modify to prevent the warning), even
           in conjunction with macros.  This also enables some language-specific warnings
           described in C++ Dialect Options and Objective-C and Objective-C++ Dialect
           Options.

           -Wall turns on the following warning flags:

           -Waddress -Warray-bounds=1 (only with -O2) -Wbool-compare -Wbool-operation
           -Wc++11-compat  -Wc++14-compat -Wchar-subscripts -Wcomment
           -Wduplicate-decl-specifier (C and Objective-C only) -Wenum-compare (in C/ObjC;
           this is on by default in C++) -Wformat -Wint-in-bool-context -Wimplicit (C and
           Objective-C only) -Wimplicit-int (C and Objective-C only)
           -Wimplicit-function-declaration (C and Objective-C only) -Winit-self (only for
           C++) -Wlogical-not-parentheses -Wmain (only for C/ObjC and unless -ffreestanding)
           -Wmaybe-uninitialized -Wmemset-elt-size -Wmemset-transposed-args
           -Wmisleading-indentation (only for C/C++) -Wmissing-braces (only for C/ObjC)
           -Wnarrowing (only for C++) -Wnonnull -Wnonnull-compare -Wopenmp-simd
           -Wparentheses -Wpointer-sign -Wreorder -Wreturn-type -Wsequence-point
           -Wsign-compare (only in C++) -Wsizeof-pointer-memaccess -Wstrict-aliasing
           -Wstrict-overflow=1 -Wswitch -Wtautological-compare -Wtrigraphs -Wuninitialized
           -Wunknown-pragmas -Wunused-function -Wunused-label -Wunused-value
           -Wunused-variable -Wvolatile-register-var

           Note that some warning flags are not implied by -Wall.  Some of them warn about
           constructions that users generally do not consider questionable, but which
           occasionally you might wish to check for; others warn about constructions that
           are necessary or hard to avoid in some cases, and there is no simple way to
           modify the code to suppress the warning. Some of them are enabled by -Wextra but
           many of them must be enabled individually.

从中可以看出, -Wall选项会显示所有的警告。

-W

man gcc中并没有找到有关 -W 的选项,从Google搜索中得知-W选项类似于-Wall,会显示警告,但只会显示编译器认为会出现错误的警告。

举个例子

代码:

#include <stdio.h>
void main(){
    int i = 3;
    float ii = i;
    printf("Hello, World!");
    return 0;
}

直接编译:

rainshaw@Ubuntu-Linux:~$ gcc main.c -o a.out
main.c: In function ‘main’:
main.c:6:12: warning: ‘return’ with a value, in function returning void
     return 0;
            ^
main.c:2:6: note: declared here
 void main(){
      ^~~~
rainshaw@Ubuntu-Linux:~$

出现了一个警告。

使用 -w参数,我们现在知道肯定没有任何警告(x

rainshaw@Ubuntu-Linux:~$ gcc -w -o a.out main.c
rainshaw@Ubuntu-Linux:~$

使用 -Wall参数:

rainshaw@Ubuntu-Linux:~$ gcc -Wall -o a.out main.c
main.c:2:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
 void main(){
      ^~~~
main.c: In function ‘main’:
main.c:6:12: warning: ‘return’ with a value, in function returning void
     return 0;
            ^
main.c:2:6: note: declared here
 void main(){
      ^~~~
main.c:4:9: warning: unused variable ‘ii’ [-Wunused-variable]
     int ii = i;
         ^~
rainshaw@Ubuntu-Linux:~$

显示了所有的警告,包括了变量ii未使用的警告。

使用-W参数:

rainshaw@Ubuntu-Linux:~$ gcc -W -o a.out main.c
main.c: In function ‘main’:
main.c:6:12: warning: ‘return’ with a value, in function returning void
     return 0;
            ^
main.c:2:6: note: declared here
 void main(){
      ^~~~
rainshaw@Ubuntu-Linux:~$

只显示了返回值不匹配的Warning

使用-W -Wall参数:

rainshaw@Ubuntu-Linux:~$ gcc -W -Wall -o a.out main.c
main.c:2:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
 void main(){
      ^~~~
main.c: In function ‘main’:
main.c:6:12: warning: ‘return’ with a value, in function returning void
     return 0;
            ^
main.c:2:6: note: declared here
 void main(){
      ^~~~
main.c:4:9: warning: unused variable ‘ii’ [-Wunused-variable]
     int ii = i;
         ^~
rainshaw@Ubuntu-Linux:~$

比单独使用-W参数多了一些警告。

后记

在Google搜索过程中,看到这样一种说法:

编译时不使用-W -Wall选项是stupid

所以以后还是都带上为好。

最后修改:2020 年 10 月 11 日
如果觉得我的文章对你有用,请随意赞赏