MSVC:fix warning: _WIN32_WINNT not defined. Defaulting to _WIN32_WINNT_MAXVER (see WinSDKVer.h)

版权声明:本文为博主原创文章,转载请注明源地址。 https://blog.csdn.net/10km/article/details/82557899

编译一个从旧版本MSVC工程时没头没脑的报了个警告(我的编译器是Visual Studio 2015):

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

意思就是没有定义_WIN32_WINNT,所以被缺省定义为_WIN32_WINNT_MAXVER
网上的解决方案都是一样的(参见这个CSDN博客:《_WIN32_WINNT not defined. Defaulting to _WIN32_WINNT_MAXVER (see WinSDKVer.h》

步骤1
编译时加入_WIN32_WINNT定义
步骤2
代码中include sdkddkver.h,比如在StdAfx.h中添加#include <sdkddkver.h>

#ifndef WINVER
#define WINVER 0x0502
#endif

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0502
#endif
#include <sdkddkver.h>

有了解决方案,总要知道为什么
为什么要加_WIN32_WINNT这个定义呢?
添加这个定义用于显式指定程序运行的平台,比如 _WIN32_WINNT=0x0502就是指定程序运行的平台为Windows Server 2003 with SP1, Windows XP with SP2.(0x0502的定义来源参见本文最后的参考资料链接)
新的VC版本(比如VS2015)不再支持过于旧的Windows平台(比如Windows 95, Windows 98, Windows ME, Windows NT or Windows 2000),如果你要的应用程序要求支持这些旧的平台,就要显式定义_WIN32_WINNT

如果不显式定义_WIN32_WINNT,只要include sdkddkver.h,在sdkddkver.h中会自动定义_WIN32_WINNT为当前MSVC定义的最高可用的windows平台(_WIN32_WINNT_MAXVER )。比如VS2015下会定义为0x0603(_WIN32_WINNT_WINBLUE),那么程序就不能运行在低于这个版本的Window系统上。

Visual C++ no longer supports targeting Windows 95, Windows 98, Windows ME, Windows NT or Windows 2000. If your WINVER or _WIN32_WINNT macros are assigned to one of these versions of Windows, you must modify the macros. When you upgrade a project that was created by using an earlier version of Visual C++, you may see compilation errors related to the WINVER or _WIN32_WINNT macros if they are assigned to a version of Windows that is no longer supported. @https://msdn.microsoft.com/en-us/library/6sehtctf.aspx

参考资料

《Using the Windows Headers》
《Modifying WINVER and _WIN32_WINNT》
《’_WIN32_WINNT’ / ‘WINVER’ : macro redefinition》
《Setting a ‘compatibility’》

猜你喜欢

转载自blog.csdn.net/10km/article/details/82557899