In a C++ function I need the compiler to choose a different block if it is compiling for a 64 bit architecture.
在C ++函数中,如果编译64位架构,我需要编译器选择不同的块。
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?
我知道为MSVC ++和g ++做一个方法,所以我会把它作为答案发布。但是我想知道是否有更好的方法(更优雅的方式适用于所有编译器/所有64位架构)。如果没有更好的方法,我应该寻找其他预定义的宏以便与其他编译器/架构兼容?
8 个解决方案
#1
9
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, use sizeof(int) == 8
.
你为什么选择一个区块而不是另一个区块?如果您的决定基于指针的大小,请使用sizeof(void *)== 8.如果您的决定基于整数的大小,请使用sizeof(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.
我的观点是,架构本身的名称应该很少有任何区别。出于您的目的,您只需检查您需要检查的内容。您的问题并不能很清楚地说明您的检查目的是什么。您要问的是类似于通过查询Windows版本来确定是否安装了DirectX。您可以使用更多便携式和通用工具。
#2
19
This works for MSVC++ and g++ :
这适用于MSVC ++和g ++:
#if defined(_M_X64) || defined(__amd64__)
// code...
#endif
#3
16
An architecture-independent way to detect 32-bit and 64-bit builds in C and C++ looks like this:
在C和C ++中检测32位和64位构建的独立于体系结构的方法如下所示:
// C
#include <stdint.h>
// C++
#include <cstdint>
#if INTPTR_MAX == INT64_MAX
// 64-bit
#elif INTPTR_MAX == INT32_MAX
// 32-bit
#else
#error Unknown pointer size or missing size macros!
#endif
#5
2
#ifdef _LP64
Works on both platforms
适用于两个平台
#6
1
Here's a good overview for Mac OS X:
这是Mac OS X的一个很好的概述:
http://developer.apple.com/documentation/Darwin/Conceptual/64bitPorting
#7
1
If you're compiling for the Windows platform, you should use:
如果您正在为Windows平台进行编译,则应使用:
#ifdef _WIN64
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.
MSVC编译器为x64和ia64平台定义了这个(你不想切断那个市场,对吗?)。我不确定gcc是否做同样的事情 - 但如果没有,那就应该。
An alternative is
另一种选择是
#ifdef WIN64
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.
这有一个微妙的区别。 WIN64(没有前导下划线)由SDK(或构建配置)定义。由于这是由SDK / build配置定义的,因此它应该与gcc一样好用。
#8
-3
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):
如果您使用Windows,最好从注册表中获取“PROCESSOR_ARCHITECTURE”环境变量,因为如果在64位操作系统(即WOW64)上运行32位进程,sizeof(PVOID)将等于4:
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SYSTEM\CurrentControlSet\\Control\\Session Manager\\Environment"), 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
LPSTR szArch = new CHAR[100];
ZeroMemory(szArch, 100);
if (RegQueryValueEx(hKey, _T("PROCESSOR_ARCHITECTURE"), NULL, NULL, (LPBYTE)szArch, &dwSize) == ERROR_SUCCESS) {
if (strcmp(szArch, "AMD64") == 0)
this->nArchitecture = 64;
else
this->nArchitecture = 32;
} else {
this->nArchitecture = (sizeof(PVOID) == 4 ? 32 : 64);
}
RegCloseKey(hKey);
}
#1
9
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, use sizeof(int) == 8
.
你为什么选择一个区块而不是另一个区块?如果您的决定基于指针的大小,请使用sizeof(void *)== 8.如果您的决定基于整数的大小,请使用sizeof(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.
我的观点是,架构本身的名称应该很少有任何区别。出于您的目的,您只需检查您需要检查的内容。您的问题并不能很清楚地说明您的检查目的是什么。您要问的是类似于通过查询Windows版本来确定是否安装了DirectX。您可以使用更多便携式和通用工具。
#2
19
This works for MSVC++ and g++ :
这适用于MSVC ++和g ++:
#if defined(_M_X64) || defined(__amd64__)
// code...
#endif
#3
16
An architecture-independent way to detect 32-bit and 64-bit builds in C and C++ looks like this:
在C和C ++中检测32位和64位构建的独立于体系结构的方法如下所示:
// C
#include <stdint.h>
// C++
#include <cstdint>
#if INTPTR_MAX == INT64_MAX
// 64-bit
#elif INTPTR_MAX == INT32_MAX
// 32-bit
#else
#error Unknown pointer size or missing size macros!
#endif
#4
#5
2
#ifdef _LP64
Works on both platforms
适用于两个平台
#6
1
Here's a good overview for Mac OS X:
这是Mac OS X的一个很好的概述:
http://developer.apple.com/documentation/Darwin/Conceptual/64bitPorting
#7
1
If you're compiling for the Windows platform, you should use:
如果您正在为Windows平台进行编译,则应使用:
#ifdef _WIN64
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.
MSVC编译器为x64和ia64平台定义了这个(你不想切断那个市场,对吗?)。我不确定gcc是否做同样的事情 - 但如果没有,那就应该。
An alternative is
另一种选择是
#ifdef WIN64
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.
这有一个微妙的区别。 WIN64(没有前导下划线)由SDK(或构建配置)定义。由于这是由SDK / build配置定义的,因此它应该与gcc一样好用。
#8
-3
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):
如果您使用Windows,最好从注册表中获取“PROCESSOR_ARCHITECTURE”环境变量,因为如果在64位操作系统(即WOW64)上运行32位进程,sizeof(PVOID)将等于4:
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SYSTEM\CurrentControlSet\\Control\\Session Manager\\Environment"), 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
LPSTR szArch = new CHAR[100];
ZeroMemory(szArch, 100);
if (RegQueryValueEx(hKey, _T("PROCESSOR_ARCHITECTURE"), NULL, NULL, (LPBYTE)szArch, &dwSize) == ERROR_SUCCESS) {
if (strcmp(szArch, "AMD64") == 0)
this->nArchitecture = 64;
else
this->nArchitecture = 32;
} else {
this->nArchitecture = (sizeof(PVOID) == 4 ? 32 : 64);
}
RegCloseKey(hKey);
}