如何检测我是否正在为 C++ 中的 64 位架构进行编译?
在 C++ 函数中,如果针对 64 位架构进行编译,我需要编译器选择不同的块。
我知道一种针对 MSVC++ 和 g++ 的方法,所以我将其作为答案发布。 不过我想知道是否有更好的方法(更优雅,适用于所有编译器/所有 64 位架构)。 如果没有更好的方法,我应该寻找哪些其他预定义宏以便与其他编译器/体系结构兼容?
In a C++ function I need the compiler to choose a different block if it is compiling for a 64 bit architecture.
I know a way to do it for MSVC++ and g++, so I'll post it as an answer. However I would like to know if there is a better way (more elegant that would work for all compilers/all 64 bits architectures). If there is not a better way, what other predefined macros should I look for in order to be compatible with other compiler/architectures?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在 C 和 C++ 中检测 32 位和 64 位构建的独立于体系结构的方法如下所示:
An architecture-independent way to detect 32-bit and 64-bit builds in C and C++ looks like this:
这适用于 MSVC++ 和 g++:
This works for MSVC++ and
g++
:为什么你选择一个区块而不是另一个区块? 如果您的决定基于指针的大小,请使用
sizeof(void*) == 8
。 如果您的决定基于整数的大小,请使用sizeof(int) == 8
。我的观点是,架构本身的名称应该很少有任何区别。 您只检查您需要检查的内容,以实现您要做的事情。 您的问题没有很清楚地说明您检查的目的是什么。 您所询问的内容类似于尝试通过查询 Windows 版本来确定是否安装了 DirectX。 您可以使用更多便携式和通用工具。
Why are you choosing one block over the other? If your decision is based on the size of a pointer, use
sizeof(void*) == 8
. If your decision is based on the size of an integer, usesizeof(int) == 8
.My point is that the name of the architecture itself should rarely make any difference. You check only what you need to check, for the purposes of what you are going to do. Your question does not cover very clearly what your purpose of the check is. What you are asking is akin to trying to determine if DirectX is installed by querying the version of Windows. You have more portable and generic tools at your disposal.
Raymond 介绍了这一点。
Raymond covers this.
如果您要针对 Windows 平台进行编译,则应该使用:
MSVC 编译器为 x64 和 ia64 平台定义了该平台(您不想排除该市场,是吗?)。 我不确定 gcc 是否也这样做 - 但如果没有,它应该这样做。
另一种选择是
有细微差别的。 WIN64(不带前导下划线)由 SDK(或构建配置)定义。 由于这是由 SDK/build 配置定义的,因此它应该与 gcc 一起工作。
If you're compiling for the Windows platform, you should use:
The MSVC compiler defines that for both x64 and ia64 platforms (you don't want to cut out that market, do you?). I'm not sure if gcc does the same - but it should if it doesn't.
An alternative is
which has a subtle difference. WIN64 (without the leading underscore) is defined by the SDK (or the build configuration). Since this is defined by the SDK/build config, it should work just as well with gcc.
适用于两个平台
Works on both platforms
以下是 Mac OS X 的详细概述:
http://developer.apple.com/documentation /达尔文/概念/64bitPorting
Here's a good overview for Mac OS X:
http://developer.apple.com/documentation/Darwin/Conceptual/64bitPorting
如果您使用 Windows,您可能最好从注册表中获取“PROCESSOR_ARCHITECTURE”环境变量,因为如果它是在 64 位操作系统(又名 WOW64)上运行的 32 位进程,则 sizeof(PVOID) 将等于 4:
If your using Windows, your probably better to get the "PROCESSOR_ARCHITECTURE" environment variable from the registry because sizeof(PVOID) will equal 4 if its a 32bit process running on a 64bit operating system (aka WOW64):