编译一个从旧版本MSVC工程时没头没脑的报了个警告(我的编译器是Visual Studio 2015):
_WIN32_WINNT not defined. Defaulting to _WIN32_WINNT_MAXVER (see WinSDKVer.h)
意思就是没有定义_WIN32_WINNT,所以被缺省定义为_WIN32_WINNT_MAXVER。
网上的解决方案都是一样的(参见这个优快云博客:《_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’》
本文解决了一个常见的编译警告问题:_WIN32_WINNT未定义,默认使用_WIN32_WINNT_MAXVER。介绍了如何通过在编译选项中加入_WIN32_WINNT定义并修改代码来解决该问题,确保程序能在指定版本的Windows平台上运行。

被折叠的 条评论
为什么被折叠?



