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
How to get rid of _WIN32_WINNT not defined warning?
提问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_MAXVER
from WinSDKVer.h) is highly unlikely to break anything.
将它定义为相同的值(与_WIN32_WINNT_MAXVER
WinSDKVer.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
#define
statement in each source file, or by specifying the/D
compiler option supported by Visual C++.For example, to set
WINVER
in your source file, use the following statement:#define WINVER 0x0502 // Windows Server 2003 with SP1, Windows XP with SP2
To set
_WIN32_WINNT
in your source file, use the following statement:#define _WIN32_WINNT 0x0502 // Windows Server 2003 with SP1, Windows XP with SP2
To set
_WIN32_WINNT
using the/D
compiler option, use the following command:cl -c /D_WIN32_WINNT=0x0502 source.cpp
您可以通过使用
#define
每个源文件中的语句或通过指定/D
Visual 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),这里就是它的去处。
Configuration Properties
C/C++
Preprocessor
Preprocessor Definitions
_WIN32_WINNT=0x0502
配置属性
C/C++
预
处理器预处理器定义
_WIN32_WINNT=0x0502
回答by Pierre
Note: the #define _WIN32_WINNT
must occur beforeany header file, including "stdafx.h".
注意:#define _WIN32_WINNT
必须出现在任何头文件之前,包括“stdafx.h”。
回答by MikeOnline
#define _WIN32_WINNT
and #define WINVER
canoccur in a header, so long as they occur before including SDKDDKVer.h
or any headers from the Windows SDK (in particular Windows.h
). Visual Studio VC++ projects typically provide a targetver.h
header, where you can include WinSDKVer.h
, define _WIN32_WINNT
, NTDDI_VERSION
and 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_WINNT
,NTDDI_VERSION
和WINVER
,然后包括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_WINNT
and WINVER
in targetver.h
(though it mentions these can be defined using command-line parameters, too).
Microsoft 文章更新 WINVER 和 _WIN32_WINNT 不遗余力地指导您定义_WIN32_WINNT
和WINVER
输入targetver.h
(尽管它提到也可以使用命令行参数定义这些)。
Note: targetver.h
gets included in certain .rc
resource files generated by Visual Studio, so this is another reason to fill out the contents of the targetver.h
file as described, above.
注意:targetver.h
包含在.rc
由 Visual Studio 生成的某些资源文件中,因此这是targetver.h
如上所述填写文件内容的另一个原因 。
Note: defining _WIN32_WINNT
, NTDDI_VERSION
and WINVER
using 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.h
and SDKDDKVer.h
.
注:定义_WIN32_WINNT
,NTDDI_VERSION
并WINVER
使用该项目的构建配置中的命令行参数或预处理指令也可以工作,但既不需要的需要,包括保健WinSDKVer.h
和SDKDDKVer.h
。
Note: when using the Windows 8.1 SDK (or newer), you may also need to define WINAPI_FAMILY
in targetver.h
depending on the specific Windows platform (server, desktop, phone, store, etc.) you need. See winapifamily.h
for details.
注:当使用的是Windows 8.1 SDK(或更新版本),你可能还需要定义WINAPI_FAMILY
在targetver.h
根据特定的Windows平台(服务器,台式机,手机,存储等),你需要的。详情请参阅winapifamily.h
。
Note: In newer Windows SDK versions, the values NTDDI_VERSION
, WINVER
and _WIN32_IE
can be set automatically based on the value you define for _WIN32_WINNT
. However, you may want to define a more specific version for NTDDI_VERSION
explicitly prior to including SDKDDKVer.h
.
注:在较新的Windows SDK版本,价值观NTDDI_VERSION
,WINVER
并且_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>