'_WIN32_WINNT' / 'WINVER' : 宏重定义

'_WIN32_WINNT' / 'WINVER' : macro redefinition

我有一个 VS2015 C++ 项目。 Windows 7 和 XP 上的应用都必须 运行。所以,我想将 _WIN32_WINNT & WINVER 设置为 _WIN32_WINNT_WINXP

我的项目 stdafx.h 是这样的:

stdafx.h

#pragma once

#include "targetver.h"

#define _WIN32_WINNT        _WIN32_WINNT_WINXP         
#define WINVER              _WIN32_WINNT_WINXP   

#include <WinSDKVer.h>

// Windows Header Files:
#include <windows.h>

编译后,我看到以下内容warning/error:

stdafx.h(12): error C2220: warning treated as error - no 'object' file generated
1>stdafx.h(12): warning C4005: '_WIN32_WINNT': macro redefinition
1>  C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\include\SDKDDKVer.h(197): note: see previous definition of '_WIN32_WINNT'
1>stdafx.h(13): warning C4005: 'WINVER': macro redefinition
1>  C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\include\SDKDDKVer.h(212): note: see previous definition of 'WINVER'

因为 #include "targetver.h" 包括 <sdkddkver.h>,它已经定义了常量 _WIN32_WINNT 和 WINVER,但构建环境尚未定义它们。

事实上,自动生成的 targetver.h 告诉您如何解决这个问题:

#pragma once

// Including SDKDDKVer.h defines the highest available Windows platform.

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

#include <SDKDDKVer.h>

简单的解决方案。只需在包含 targetver.h 之前定义这些常量。您可能必须使用 XP 的实际文字值,因为您没有包含定义

的头文件

像这样:

// x501 is XP
#define _WIN32_WINNT        0x0501         
#define WINVER              0x0501  

#include "targetver.h"
#include <windows.h>

尝试

#include <winsdkver.h>
#undef _WIN32_WINNT
#define _WIN32_WINNT _WIN32_WINNT_WINXP
#include <SDKDDKVer.h>

你 #include 所以你可以使用 _WIN32_WIN_WINXP。但是,您需要 #undef _WIN32_WINNT 因为它是在 中定义的。这样,当您使用#define _WIN32_WINNT _WIN32_WINNT_WINXP 时,您不会得到 _WIN32_WINNT 警告的重新定义。