C++ “g++ -Wall test.cpp -o test”中的“-Wall”有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2408038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:23:44  来源:igfitidea点击:

What does "-Wall" in "g++ -Wall test.cpp -o test" do?

c++g++

提问by steven

-o changes the output filename (I found that using --help)

-o 更改输出文件名(我发现使用 --help)

But I can't find out what -Walldoes?

但我不知道-Wall是什么?

回答by MatrixFrog

It's short for "warn all" -- it turns on (almost) all the warnings that g++ can tell you about. Typically a good idea, especially if you're a beginner, because understanding and fixing those warnings can help you fix lots of different kinds of problems in your code.

它是“warn all”的缩写——它打开(几乎)所有 g++ 可以告诉你的警告。通常是个好主意,特别是如果您是初学者,因为理解和修复这些警告可以帮助您修复代码中的许多不同类型的问题。

回答by rurban

See man gcc.

参见 man gcc。

-Wallturns on these warnings:

-Wall打开这些警告:

-Waddress -Warray-bounds (only with -O2) -Wc++0x-compat -Wchar-subscripts
-Wenum-compare (in C/Objc; this is on by default in C++) -Wimplicit-int (C and
 Objective-C only) -Wimplicit-function-declaration (C and Objective-C only) 
-Wcomment -Wformat -Wmain (only for C/ObjC and unless -ffreestanding) 
-Wmissing-braces -Wnonnull -Wparentheses -Wpointer-sign -Wreorder -Wreturn-type 
-Wsequence-point -Wsign-compare (only in C++) -Wstrict-aliasing 
-Wstrict-overflow=1 -Wswitch -Wtrigraphs -Wuninitialized -Wunknown-pragmas 
-Wunused-function -Wunused-label -Wunused-value -Wunused-variable 
-Wvolatile-register-var

-Wextracontains:

-Wextra包含:

-Wclobbered -Wempty-body -Wignored-qualifiers -Wmissing-field-initializers
-Wmissing-parameter-type (C only) -Wold-style-declaration (C only) -Woverride-init
-Wsign-compare -Wtype-limits -Wuninitialized -Wunused-parameter (only with -Wunused
 or -Wall) -Wunused-but-set-parameter (only with -Wunused or -Wall)

There are many more warnings which you have to turn on explicitly.

您必须明确打开更多警告。

E.g. for our C code we use:

例如,对于我们使用的 C 代码:

-Wall -Wextra -Waggregate-return -Wcast-align -Wcast-qual -Wdisabled-optimization -Wdiv-by-zero -Wendif-labels -Wformat-extra-args -Wformat-nonliteral -Wformat-security -Wformat-y2k -Wimplicit -Wimport -Winit-self -Winline -Winvalid-pch -Wjump-misses-init -Wlogical-op -Werror=missing-braces -Wmissing-declarations -Wno-missing-format-attribute -Wmissing-include-dirs -Wmultichar -Wpacked -Wpointer-arith -Wreturn-type -Wsequence-point -Wsign-compare -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch -Wswitch-default -Werror=undef -Wno-unused -Wvariadic-macros -Wwrite-strings -Wc++-compat -Werror=declaration-after-statement -Werror=implicit-function-declaration -Wmissing-prototypes -Werror=nested-externs -Werror=old-style-definition -Werror=strict-prototypes

-Wall -Wextra -Waggregate-return -Wcast-align -Wcast-qual -Wdisabled-optimization -Wdiv-by-zero -Wendif-labels -Wformat-extra-args -Wformat-nonliteral -Wformat-security -Wformat-y2k -Wimplicit -Wimport -Winit-self -Winline -Winvalid-pch -Wjump-misses-init -Wlogical-op -Werror=missing-braces -Wmissing-declarations -Wno-missing-format-attribute -Wmissing-include-dirs -Wmultichar -Wpacked -Wpointer-arith -Wreturn-type -Wsequence-point -Wsign-compare -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch -Wswitch-default -Werror=undef -Wno-unused -Wvariadic-macros -Wwrite-strings -Wc++ -compat -Werror=declaration-after-statement -Werror=implicit-function-declaration -Wmissing-prototypes -Werror=nested-externs -Werror=old-style-definition -Werror=strict-prototypes

or just the set of warnings with https://www.gnu.org/software/autoconf-archive/ax_compiler_flags.html

或者只是带有https://www.gnu.org/software/autoconf-archive/ax_compiler_flags.html的一组警告

回答by DevSolar

Sadly enough none of the answers is quoting the actually relevant part of manual, which really brings it to a point:

遗憾的是,没有一个答案引用手册中实际相关的部分,这确实说明了这一点:

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.

[...]

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 -Wextrabut many of them must be enabled individually.

这会启用所有关于某些用户认为有问题的结构的警告,并且很容易避免(或修改以防止警告),即使与宏结合使用也是如此。

[...]

请注意,某些警告标志并不隐含在-Wall. 其中一些警告用户通常不认为有问题的结构,但有时您可能希望检查;其他人警告在某些情况下必要或难以避免的结构,并且没有简单的方法来修改代码以抑制警告。其中一些由 启用,-Wextra但其中许多必须单独启用。

Ergo:

因此:

  • -Walldoes notmean "all warnings".
  • It does also notmean "(almost) all", not by a long shot.
  • It doesmean a set of individual options that is bound to change.
  • -Wall不会意味着“所有警告”。
  • 也不意味着“(几乎)全部”,不是远景。
  • 确实意味着一组必然会改变的单个选项。

Bottom line, it is about the absolute minimum of warnings you should set. While -Wall -Wextrais better, it's still not making use of all the error checking your compiler can do for you.

最重要的是,它是您应该设置的警告的绝对最小值。虽然-Wall -Wextra更好,但它仍然没有利用您的编译器可以为您做的所有错误检查。



Personally I wouldn't go for less than -Wall -Wextra -Wfloat-equal -Wundef -Wcast-align -Wwrite-strings -Wlogical-op -Wmissing-declarations -Wredundant-decls -Wshadow -Woverloaded-virtual. All my current projects actually use a list of warnings longer than that (without triggering any of them). And I do check the manual on every major release for new options. The compiler is your friend. Use whatever diagnostics it can offer you.

就个人而言,我不会低于-Wall -Wextra -Wfloat-equal -Wundef -Wcast-align -Wwrite-strings -Wlogical-op -Wmissing-declarations -Wredundant-decls -Wshadow -Woverloaded-virtual. 我当前的所有项目实际上都使用了比这更长的警告列表(不触发任何警告)。而且我确实检查了每个主要版本的手册以获取新选项。编译器是你的朋友。使用它可以为您提供的任何诊断。

回答by AProgrammer

It enables warnings which are deemed useful andeasy to avoid at the source by gcc writers. There is also -W (-Wextra in newer releases) which are deemed useful but for which work-arounding false positives can be difficult or result in clumsy code.

它启用了 gcc 编写者认为有用易于在源代码中避免的警告。还有 -W(新版本中的 -Wextra)被认为很有用,但解决误报可能很困难或导致代码笨拙。

gcc has also a bunch of other warnings, generally less useful. See http://gcc.gnu.org/onlinedocs/gcc-4.4.3/gcc/Warning-Options.html#Warning-Options

gcc 还有一堆其他警告,通常不太有用。请参阅http://gcc.gnu.org/onlinedocs/gcc-4.4.3/gcc/Warning-Options.html#Warning-Options

回答by Yuval

It enables most warning messages.

enables most warning messages

You can find out more if you use g++ --help=warnings.

如果您使用g++ --help=warnings.

回答by Bjorn J

It enables all warnings. (reads as "Warning All")

它启用所有警告。(读作“警告所有人”)

回答by Kirill V. Lyadvinsky

It shows all warnings. I'd recommend also use -pedanticto warn about some non-conformant parts of code.

它显示所有警告。我建议也使用-pedantic警告代码的一些不符合标准的部分。