windows 我应该在 64 位版本中同时定义 _WIN32 和 _WIN64 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6679396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:34:49  来源:igfitidea点击:

Should I define both _WIN32 and _WIN64 in 64bit build?

windowsvisual-studiovisual-c++configuration32bit-64bit

提问by Benjamin

When we add a 64bit configuration from a 32bit project that has already existed, Visual Studio copies the 32bit configurations by default. VS even copies _WIN32
All my 64bit projects define also _WIN32now, despite they(64bit PEs) never can run on 32bit Windows. It is very uncomfortable to me.

当我们从已经存在的 32 位项目中添加 64 位配置时,Visual Studio 默认会复制 32 位配置。VS 甚至复制_WIN32
我所有的 64 位项目现在也定义了_WIN32,尽管它们(64 位 PE)永远不能在 32 位 Windows 上运行。这对我来说很不舒服。

I'd like to remove the _WIN32if it doesn't have any problem. I'm not sure about that.
Is it okay if I remove the _WIN32definition?

如果没有任何问题,我想删除_WIN32。对此我不确定。
如果我删除_WIN32定义可以吗?

回答by Hans Passant

_WIN32 doesn't mean what you think it does. It means "I am using the Windows API". The 32 postfix was added back in the days of Windows NT 3.1 to make it distinct from the 16-bit API that was used in Windows version 3. This term has fallen out of favor because of the bitness problem. You can see this at stackoverflow.com, the [win32] tag takes you to [winapi].

_WIN32 并不意味着你认为它的作用。这意味着“我正在使用 Windows API”。32 后缀是在 Windows NT 3.1 时代重新添加的,以使其区别于 Windows 版本 3 中使用的 16 位 API。由于位问题,该术语已经失宠。您可以在 stackoverflow.com 上看到这一点,[win32] 标签会将您带到 [winapi]。

Don't remove it, you are using the Windows API.

不要删除它,您正在使用 Windows API。

回答by Frédéric Hamidi

The documentation for the predefined macrossays:

预定义宏的文档说:

_WIN32:Defined for applications for Win32 and Win64. Always defined.

_WIN64:Defined for applications for Win64.

_WIN32:为 Win32 和 Win64 应用程序定义。始终定义。

_WIN64:为 Win64 应用程序定义。

So not only should _WIN32alwaysbe defined, it does not cause any problem in 64-bit applications. Therefore, I'd suggest you don't remove it.

因此不仅应该_WIN32始终定义,而且在 64 位应用程序中不会引起任何问题。因此,我建议您不要删除它。

回答by Ben Voigt

You should never define either of them.

你永远不应该定义它们中的任何一个。

The toolchain (compiler + system headers) will define them as appropriate.

工具链(编译器 + 系统头文件)将根据需要定义它们。

回答by Dana Yan

_WIN32:Defined for applications for Win32 and Win64. Always defined.

_WIN64:Defined for applications for Win64.

_WIN32:为Win32和Win64应用定义。始终定义。

_WIN64:为Win64应用定义。

More detail: Predefined Macros.

更多细节:预定义宏

To put it simply, WIN32/_WIN32 is used to tell whether you are using Windows(For crossing system application), while _WIN64 is used to tell the compiling environment is x86 or x64.

简单来说,WIN32/_WIN32是用来判断你是否使用的是Windows(对于跨系统应用),而_WIN64是用来判断编译环境是x86还是x64。

If you want to know whether your application is running under Windows x64, you should use Windows API IsWow64Process .

如果您想知道您的应用程序是否在 Windows x64 下运行,您应该使用 Windows API IsWow64Process