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

编译一个从旧版本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’》

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值