简体   繁体   中英

How to get rid of _WIN32_WINNT not defined warning?

_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?

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 .

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

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

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

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

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

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

Solving in 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.

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

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

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.

在此处输入图片说明

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

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

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.

#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.

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

#define _WIN32_WINNT and #define WINVER can occur 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:

// 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_WINNT goes 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).

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.

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 .

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM