C++ 如何摆脱 _WIN32_WINNT 未定义警告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12871513/
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 16:42:50  来源:igfitidea点击:

How to get rid of _WIN32_WINNT not defined warning?

c++visual-c++

提问by User982367637

_WIN32_WINNT not defined. Defaulting to _WIN32_WINNT_MAXVER (see WinSDKVer.h)

This error keeps popping up during my compilation. It doesn't seem to affect compilation, but how should I get rid of it? Am I risking anything by ignoring it?

这个错误在我的编译过程中不断弹出。它似乎不影响编译,但我应该如何摆脱它?我是否会因为忽略它而冒任何风险?

采纳答案by Roman R.

It is defined for you through WinSDKVer.h. So just define it explicitly on the top of your source code (e.g. in the beginning of stdafx.h) and you will get rid of the warning.

它是通过 WinSDKVer.h 为您定义的。因此,只需在源代码的顶部(例如在 stdafx.h 的开头)明确定义它,您就会摆脱警告。

Having it defined to the same value (as compared to _WIN32_WINNT_MAXVERfrom WinSDKVer.h) is highly unlikely to break anything.

将它定义为相同的值(与_WIN32_WINNT_MAXVERWinSDKVer.h相比)极不可能破坏任何东西。

For example, WinSDKVer.h of Windows? Software Development Kit (SDK) for Windows 7 and .NET Framework 3.5 Service Pack 1 contains:

比如Windows的WinSDKVer.h?适用于 Windows 7 和 .NET Framework 3.5 Service Pack 1 的软件开发工具包 (SDK) 包含:

// This list contains the highest version constants supported by content 
// in the Windows SDK.

// [...]
#define _WIN32_WINNT_MAXVER     0x0601

回答by user93353

Set it to the oldest Windows Operating System you want your program to run on. The possible values are given in this MSDN article, Using the Windows Headers.

将其设置为您希望程序在其上运行的最旧的 Windows 操作系统。可能的值在这篇 MSDN 文章Using the Windows Headers 中给出

You can define these symbols by using the #definestatement in each source file, or by specifying the /Dcompiler option supported by Visual C++.

For example, to set WINVERin your source file, use the following statement:

 #define WINVER 0x0502    // Windows Server 2003 with SP1, Windows XP with SP2

To set _WIN32_WINNTin your source file, use the following statement:

#define _WIN32_WINNT 0x0502    // Windows Server 2003 with SP1, Windows XP with SP2

To set _WIN32_WINNTusing the /Dcompiler option, use the following command:

cl -c /D_WIN32_WINNT=0x0502 source.cpp

您可以通过使用#define每个源文件中的语句或通过指定/DVisual C++ 支持的编译器选项来定义这些符号。

例如,要WINVER在源文件中进行设置,请使用以下语句:

 #define WINVER 0x0502    // Windows Server 2003 with SP1, Windows XP with SP2

_WIN32_WINNT在源文件中进行设置,请使用以下语句:

#define _WIN32_WINNT 0x0502    // Windows Server 2003 with SP1, Windows XP with SP2

_WIN32_WINNT使用/D编译器选项进行设置,请使用以下命令:

cl -c /D_WIN32_WINNT=0x0502 source.cpp

回答by Ivan

Solving in VS2019

在 VS2019 中求解

Ways to solve this and a link to possible values to use can be found here in the super answer by user93353 which I used to solve the problem.

可以在 user93353 的超级答案中找到解决此问题的方法以及指向要使用的可能值的链接,我用来解决该问题。

https://stackoverflow.com/a/12871547/3070485

https://stackoverflow.com/a/12871547/3070485

However, after reading the solution, I set my compiler option in my IDE which is Visual Studio 2019.

但是,在阅读解决方案后,我在 Visual Studio 2019 的 IDE 中设置了编译器选项。

For anyone wanting to set it there quickly and wanting to know the location (as these things change from IDE release to release, or maybe someone is more familiar with another IDE), here is where it went.

对于任何想要快速将它设置在那里并想要知道位置的人(因为这些东西会随着 IDE 的发布而变化,或者可能有人更熟悉另一个 IDE),这里就是它的去处。

enter image description here

在此处输入图片说明

Configuration Properties
C/C++
Preprocessor
Preprocessor Definitions
_WIN32_WINNT=0x0502

配置属性
C/C++

处理器预处理器定义
_WIN32_WINNT=0x0502

回答by Pierre

Note: the #define _WIN32_WINNTmust occur beforeany header file, including "stdafx.h".

注意:#define _WIN32_WINNT必须出现任何头文件之前,包括“stdafx.h”。

回答by MikeOnline

#define _WIN32_WINNTand #define WINVERcanoccur in a header, so long as they occur before including SDKDDKVer.hor any headers from the Windows SDK (in particular Windows.h). Visual Studio VC++ projects typically provide a targetver.hheader, where you can include WinSDKVer.h, define _WIN32_WINNT, NTDDI_VERSIONand WINVER, and then include SDKDDKVer.h. Comments within this file say:

#define _WIN32_WINNT并且#define WINVER可以出现在标头中,只要它们出现在包含SDKDDKVer.h或来自 Windows SDK 的任何标头之前(特别是Windows.h)。Visual Studio的VC ++项目通常提供一个targetver.h头,在那里你可以包括WinSDKVer.h,定义_WIN32_WINNTNTDDI_VERSIONWINVER,然后包括SDKDDKVer.h。该文件中的评论说:

// If you wish to build your application for a previous Windows platform, include `WinSDKVer.h`
// and set the _WIN32_WINNT macro to the platform you wish to support before including `SDKDDKVer.h`.

The Microsoft article Update WINVER and _WIN32_WINNTgoes out of its way to instruct you to define _WIN32_WINNTand WINVERin targetver.h(though it mentions these can be defined using command-line parameters, too).

Microsoft 文章更新 WINVER 和 _WIN32_WINNT 不遗余力地指导您定义_WIN32_WINNTWINVER输入targetver.h(尽管它提到也可以使用命令行参数定义这些)。

Note: targetver.hgets included in certain .rcresource files generated by Visual Studio, so this is another reason to fill out the contents of the targetver.hfile as described, above.

注意:targetver.h包含在.rc由 Visual Studio 生成的某些资源文件中,因此这是targetver.h如上所述填写文件内容的另一个原因 。

Note: defining _WIN32_WINNT, NTDDI_VERSIONand WINVERusing command-line parameters or preprocessor directives within the project's build configurations can also work, but neither takes care of the need to include WinSDKVer.hand SDKDDKVer.h.

注:定义_WIN32_WINNTNTDDI_VERSIONWINVER使用该项目的构建配置中的命令行参数或预处理指令也可以工作,但既不需要的需要,包括保健WinSDKVer.hSDKDDKVer.h

Note: when using the Windows 8.1 SDK (or newer), you may also need to define WINAPI_FAMILYin targetver.hdepending on the specific Windows platform (server, desktop, phone, store, etc.) you need. See winapifamily.hfor details.

注:当使用的是Windows 8.1 SDK(或更新版本),你可能还需要定义WINAPI_FAMILYtargetver.h根据特定的Windows平台(服务器,台式机,手机,存储等),你需要的。详情请参阅winapifamily.h

Note: In newer Windows SDK versions, the values NTDDI_VERSION, WINVERand _WIN32_IEcan be set automatically based on the value you define for _WIN32_WINNT. However, you may want to define a more specific version for NTDDI_VERSIONexplicitly prior to including SDKDDKVer.h.

注:在较新的Windows SDK版本,价值观NTDDI_VERSIONWINVER并且_WIN32_IE可以自动根据您定义的值设置_WIN32_WINNT。但是,您可能希望NTDDI_VERSION在包含SDKDDKVer.h.

回答by Richard Frank

If using MFC: My preferred answer is to include windows.h. But if you are using MFC, you cannot do this*. To avoid defining WINVER in your project, instead you can include sdkddkver.h before afx.h.

如果使用 MFC:我的首选答案是包含 windows.h。但是,如果您使用的是 MFC,则不能这样做*。为了避免在您的项目中定义 WINVER,您可以在 afx.h 之前包含 sdkddkver.h。

#include <sdkddkver.h>
#include <afx.h>

The sdkddkver system header sets WINVER and _WIN32_WINNT so you don't need to add it to your project. This does the same as windows.h for versioning windows SDK/DDK from the system headers.

sdkddkver 系统标头设置了 WINVER 和 _WIN32_WINNT,因此您无需将其添加到您的项目中。这与 windows.h 相同,用于从系统头文件版本控制 windows SDK/DDK。

*if you include windows.h before afx.h you get: #error: WINDOWS.H already included. MFC apps must not #include <Windows.h>

*如果在 afx.h 之前包含 windows.h,则会得到: #error: WINDOWS.H already included. MFC apps must not #include <Windows.h>