/w、/W0、/W1、/W2、/W3、/W4、/w1、/w2、/w3、/w4、/Wall、/wd、/we、/wo、/Wv、/WX(警告级别)/w, /W0, /W1, /W2, /W3, /W4, /w1, /w2, /w3, /w4, /Wall, /wd, /we, /wo, /Wv, /WX (Warning level)

指定编译器如何为给定编译生成警告。Specifies how the compiler generates warnings for a given compilation.

语法Syntax

/w/w
/W0/W0
/W1/W1
/W2/W2
/W3/W3
/W4/W4
/Wall/Wall
/Wv[ 版本] /Wv[:version]
/Wx/WX
/w1警告/w1warning
/w2警告/w2warning
/w3警告/w3warning
/w4警告/w4warning
/wd警告/wdwarning
/we警告/wewarning
/wo警告/wowarning

备注Remarks

警告选项指定要显示的编译器警告和整个编译的警告行为。The warning options specify which compiler warnings to display and the warning behavior for the entire compilation.

警告选项和相关参数在下表中进行了说明:The warning options and related arguments are described in the following tables:

选项Option 描述Description
/w/w 禁止显示所有编译器警告。Suppresses all compiler warnings.
/W0/W0

/W1/W1

/W2/W2

/W3/W3

/W4/W4
指定编译器生成的警告级别。Specifies the level of warnings to be generated by the compiler. 有效的警告等级范围为0到4:Valid warning levels range from 0 to 4:
/W0禁止显示所有警告。/W0 suppresses all warnings. 它与 /w等效。It's equivalent to /w.
/W1显示等级1(严重)警告。/W1 displays level 1 (severe) warnings. /W1是命令行编译器中的默认设置。/W1 is the default setting in the command-line compiler.
/W2显示 level 1 和 level 2 (重大)警告。/W2 displays level 1 and level 2 (significant) warnings.
/W3显示1级、2级和3级(生产质量)警告。/W3 displays level 1, level 2, and level 3 (production quality) warnings. /W3是 IDE 中的默认设置。/W3 is the default setting in the IDE.
/W4显示级别1、级别2和级别3警告以及默认情况下未关闭的所有级别4(信息)警告。/W4 displays level 1, level 2, and level 3 warnings, and all level 4 (informational) warnings that aren't off by default. 建议使用此选项来提供不起毛的警告。We recommend that you use this option to provide lint-like warnings. 对于新项目,最好在所有编译中使用 /W4For a new project, it may be best to use /W4 in all compilations. 此选项有助于确保最少的硬查找代码缺陷。This option helps ensure the fewest possible hard-to-find code defects.
/Wall/Wall 显示 /W4显示的所有警告以及 /W4不包括的所有其他警告,例如默认情况下处于关闭状态的警告。Displays all warnings displayed by /W4 and all other warnings that /W4 doesn't include—for example, warnings that are off by default. 有关详细信息,请参阅默认情况下处于关闭状态的编译器警告For more information, see Compiler warnings that are off by default.
/Wv[ : version]/Wv[:version] 仅显示版本编译器版本及更早版本中引入的警告。Displays only warnings introduced in the version compiler version and earlier. 当你迁移到较新版本的编译器时,可以使用此选项取消代码中的新警告。You can use this option to suppress new warnings in code when you migrate to a newer version of the compiler. 它使您可以在修复现有的生成过程时维持现有的生成过程。It lets you maintain your existing build process while you fix them. 可选参数版本采用nn[形式。mm[。aaaaa-bbbbb-ccccc-dddddd-eeeeee]],其中nn是主版本号, mm是可选次版本号, aaaaa-bbbbb-ccccc-dddddd-eeeeee是编译器的可选生成号。The optional parameter version takes the form nn[.mm[.bbbbb]], where nn is the major version number, mm is the optional minor version number, and bbbbb is the optional build number of the compiler. 例如,使用 /Wv: 17只显示 Visual Studio 2012 (主版本17)或更早版本中引入的警告。For example, use /Wv:17 to display only warnings introduced in Visual Studio 2012 (major version 17) or earlier. 也就是说,它显示的是版本号为17或更低的任何编译器版本的警告。That is, it displays warnings from any version of the compiler that has a major version number of 17 or less. 它抑制 Visual Studio 2013 (主要版本18)和更高版本中引入的警告。It suppresses warnings introduced in Visual Studio 2013 (major version 18) and later. 默认情况下, /Wv使用当前编译器版本号,不会禁止显示任何警告。By default, /Wv uses the current compiler version number, and no warnings are suppressed. 有关编译器版本禁止显示的警告的信息,请参阅编译器警告(按编译器版本)。For information about which warnings are suppressed by compiler version, see Compiler warnings by compiler version.
/WX/WX 将所有编译器警告视为错误。Treats all compiler warnings as errors. 对于新项目,最好在所有编译中使用 /wx ;解决所有警告可确保最少的硬查找代码缺陷。For a new project, it may be best to use /WX in all compilations; resolving all warnings ensures the fewest possible hard-to-find code defects.

链接器还具有 /wx选项。The linker also has a /WX option. 有关详细信息,请参阅 /WX(将链接器警告视为错误)For more information, see /WX (Treat linker warnings as errors).

以下选项互相排斥。The following options are mutually exclusive with each other. 此组中指定的最后一个选项是应用的选项:The last option that's specified from this group is the one applied:

选项Option 描述Description
/w1nnnn/w1nnnn

/w2nnnn/w2nnnn

/w3nnnn/w3nnnn

/w4nnnn/w4nnnn
为_nnnn_指定的警告编号设置警告等级。Sets the warning level for the warning number specified by nnnn. 通过这些选项,可以在设置特定警告级别时更改该警告的编译器行为。These options let you change the compiler behavior for that warning when a specific warning level is set. 可以结合使用这些选项和其他警告选项来强制执行你自己的警告编码标准,而不是 Visual Studio 提供的默认值。You can use these options in combination with other warning options to enforce your own coding standards for warnings, rather than the default ones provided by Visual Studio.

例如, /w34326使 C4326 生成为等级3警告而不是级别1。For example, /w34326 causes C4326 to be generated as a level 3 warning instead of level 1. 如果使用 /w34326选项和 /W2选项进行编译,则不会生成警告 C4326。If you compile by using both the /w34326 option and the /W2 option, warning C4326 isn't generated.
/wdnnnn/wdnnnn 取消_nnnn_指定的编译器警告。Suppresses the compiler warning that is specified by nnnn.

例如, /wd4326禁止显示编译器警告 C4326。For example, /wd4326 suppresses compiler warning C4326.
/wennnn/wennnn 将_nnnn_指定的编译器警告视为错误。Treats the compiler warning that is specified by nnnn as an error.

例如, /we4326导致编译器将警告号 C4326 视为错误。For example, /we4326 causes warning number C4326 to be treated as an error by the compiler.
/wonnnn/wonnnn 报告由_nnnn_指定的编译器警告。Reports the compiler warning that is specified by nnnn only once.

例如, /wo4326使警告 C4326 仅在编译器第一次遇到时报告一次。For example, /wo4326 causes warning C4326 to be reported only once, the first time it's encountered by the compiler.

如果在创建预编译标头时使用任何警告选项,则会保留这些设置。If you use any warning options when you create a precompiled header, it keeps those settings. 使用预编译标头会使这些相同的警告选项再次生效。Using the precompiled header puts those same warning options in effect again. 若要重写预编译标头警告选项,请在命令行上设置其他警告选项。To override the precompiled header warning options, set another warning option on the command line.

您可以使用#pragma warning指令来控制在编译时在特定源文件中报告的警告级别。You can use a #pragma warning directive to control the level of warning that's reported at compile time in specific source files.

源代码中的警告杂注指令不受 /w选项的影响。Warning pragma directives in source code are unaffected by the /w option.

生成错误文档描述了警告和警告级别,并指示某些语句可能不会按预期方式编译。The build errors documentation describes the warnings and warning levels, and indicates why certain statements may not compile as you intend.

在 Visual Studio 开发环境中设置编译器选项To set the compiler options in the Visual Studio development environment

  1. 打开项目的“属性页” 对话框。Open the project's Property Pages dialog box. 有关详细信息,请参阅在 Visual Studio 中设置 C++ 编译器和生成属性For details, see Set C++ compiler and build properties in Visual Studio.

  2. 若要设置 "/W0"、" /W1"、" /W2"、" /W3"、" /W4 "、"/Wall" 、" /Wv"" /WX- " 选项,请选择 "配置属性 ** > C++ > "**To set the /W0, /W1, /W2, /W3, /W4, /Wall, /Wv, /WX, or /WX- options, select Configuration Properties > C/C++ > General.

    • 若要设置 /W0/W1/W2/W3/W4/Wall选项,请修改警告等级属性。To set the /W0, /W1, /W2, /W3, /W4, or /Wall options, modify the Warning Level property.

    • 若要设置 /wx/WX- 选项,请修改 "将警告视为错误" 属性。To set the /WX or /WX- options, modify the Treat Warnings as Errors property.

    • 若要设置 /Wv选项的版本,请在 "警告版本" 属性中输入编译器版本号。To set the version for the /Wv option, enter the compiler version number in the Warning Version property.

  3. 若要设置 " /wd " 或 " /we " 选项,请选择 "配置属性" > C/C++ > "高级" 属性页。To set the /wd or /we options, select the Configuration Properties > C/C++ > Advanced property page.

    • 若要设置 /wd选项,请选择 "禁用特定的警告属性" 下拉控件,然后选择 "编辑"。To set the /wd option, select the Disable Specific Warnings property dropdown control and then choose Edit. 在 "禁用特定警告" 对话框的 "编辑" 框中,输入警告编号。In the edit box in the Disable Specific Warnings dialog, enter the warning number. 若要输入多个警告,请使用分号( ; )分隔值。To enter more than one warning, separate the values by using a semicolon (;). 例如,若要禁用 C4001 和 C4010,请输入4001; 4010For example, to disable both C4001 and C4010, enter 4001;4010. 选择 "确定" 保存更改并返回到 "属性页" 对话框。Choose OK to save your changes and return to the Property Pages dialog.

    • 若要设置 /we选项,请选择 "将特定警告视为错误" 下拉控件,然后选择 "编辑"。To set the /we option, Select the Treat Specific Warnings As Errors property dropdown control and then choose Edit. 在 "将特定警告视为错误" 对话框中的 "编辑" 框中,输入警告编号。In the edit box in the Treat Specific Warnings As Errors dialog, enter the warning number. 若要输入多个警告,请使用分号( ; )分隔值。To enter more than one warning, separate the values by using a semicolon (;). 例如,若要将 C4001 和 C4010 视为错误,请输入4001; 4010For example, to treat both C4001 and C4010 as errors, enter 4001;4010. 选择 "确定" 保存更改并返回到 "属性页" 对话框。Choose OK to save your changes and return to the Property Pages dialog.

  4. 若要设置 /wo选项,请选择 "配置属性" > C++ C/ > "命令行" 属性页。To set the /wo option, select the Configuration Properties > C/C++ > Command Line property page. 在 "附加选项" 框中输入编译器选项。Enter the compiler option in the Additional Options box.

  5. 选择“确定”以保存更改。Choose OK to save your changes.

以编程方式设置编译器选项To set the compiler option programmatically

另请参阅See also

MSVC 编译器选项MSVC compiler options
MSVC 编译器命令行语法MSVC compiler command-line syntax