2016-12-29 81 views
1

我有一個VS2015 C++項目。應用程序必須在Windows 7和XP上運行。所以,我想設置_WIN32_WINNT & WINVER_WIN32_WINNT_WINXP'_WIN32_WINNT'/'WINVER':宏重定義

這是怎麼我的項目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> 

在編譯時,我看到下面的警告/錯誤:

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' 
+0

你不需要直接'#定義_WIN32_WINNT」 - 簡單地刪除這個 – RbMm

+0

或添加這一行'#define _CHICAGO_' – RbMm

+1

請看一下targetver.h,按照指示操​​作。 –

回答

2

因爲#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> 
+2

是否有像_WIN32_WINNT_WINXP這樣的名稱替代0x0501等數字常量的解決方案? – Liviu

+0

@Liviu - 你需要的一切都在'' – selbie

+2

是的,但是我必須在包含''之前定義'_WIN32_WINNT',不是嗎?並接受警告'C4005:'_WIN32_WINNT':宏重新定義 – Liviu