在64位上迁移32位应用程序的问题

nov*_*ice -1 c++ 32bit-64bit

我试图使用visual studio 2010将现有的c ++ 32代码迁移到Windows7上的64代码.之前从未进行64位编译.在互联网参考的帮助下,我做了64位编译的设置.像VS2010与64位编译器等和其他配置更改.在预处理器中,我删除了WIN32并添加了WIN64.我有一些其他预处理器,如OS_WIN_32和其他一些在我的代码中特定的.在使用WIN32的代码中,我添加了额外条件为|| WIN64这只是为了确保应用程序应该使用win32和win64进行编译.当我尝试编译代码时,我收到编译错误说

致命错误C1189:#error:只应定义一个WIN32和WIN64符号

此错误来自本地代码,我们检查是否定义了WIN32和WIN64.该代码如下所示.

#if defined WIN32 && defined WIN64
# error Only one of the WIN32 and WIN64 symbols should be defined
#endif
Run Code Online (Sandbox Code Playgroud)

在VS2010中,如果未启用宏,则宏内的代码将变为灰色.在我的代码中,上面的错误也是灰色的.但我仍然得到那个错误.

我添加WIN64的代码包括windows.h.供参考givine如下.

#if defined WIN32 || defined WIN64
#include <windows.h>
#include <process.h>
#endif
Run Code Online (Sandbox Code Playgroud)

所以我的问题是为什么我收到这个错误?我们不应该为64位编译添加windows.h.我尝试通过评论这个包含,但我得到其他错误与代码中使用的HANDLE.如果我去WIN32定义VS2010指向windef.h文件中的定义.此文件存在于Microsoft SDKs\windows\v7.0A\include文件夹中,即不是我的本地代码.对于依据下面给出的定义.

#ifndef WIN32
#define WIN32
#endif
Run Code Online (Sandbox Code Playgroud)

所以我想知道为什么编译器同时获得预处理器WIN32和WIN64.

在此先感谢您的帮助.

rub*_*nvb 6

你不应该自己定义.应该用来检查这个的宏是

_WIN32 // always defined for Windows apps
_WIN64 // only defined for x64 compilation
Run Code Online (Sandbox Code Playgroud)

这些由编译器定义(参见此处).

通常,IDE会将未加前缀的宏添加到命令行,以免让使用未记录的未加前缀版本的旧项目无法构建.当存在记录的备选方案时,它们工作的事实不是使用它们的理由.


归结为:

#ifdef _WIN32
  // We're on Windows, yay!
#ifdef _WIN64
  // We're on x64! Yay!
#else // _WIN64
  // We're on x86 (or perhaps IA64, but that one doesn't matter anymore). Yay!
#endif // _WIN64
#else // _WIN32
  // We're not on Windows, maybe WindowsCE or WindowsPhone stuff, otherwise some other platform
 #endif
Run Code Online (Sandbox Code Playgroud)