如何检测我是否正在为 C++ 中的 64 位架构进行编译?

发布于 2024-07-05 20:52:55 字数 178 浏览 5 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

清欢 2024-07-12 20:52:55

在 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

An architecture-independent way to detect 32-bit and 64-bit builds in C and C++ looks like this:

// 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
凉城凉梦凉人心 2024-07-12 20:52:55

这适用于 MSVC++ 和 g++:

#if defined(_M_X64) || defined(__amd64__)
  // code...
#endif

This works for MSVC++ and g++:

#if defined(_M_X64) || defined(__amd64__)
  // code...
#endif
﹂绝世的画 2024-07-12 20:52:55

为什么你选择一个区块而不是另一个区块? 如果您的决定基于指针的大小,请使用 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, use 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.

你的背包 2024-07-12 20:52:55

如果您要针对 Windows 平台进行编译,则应该使用:

#ifdef _WIN64

MSVC 编译器为 x64 和 ia64 平台定义了该平台(您不想排除该市场,是吗?)。 我不确定 gcc 是否也这样做 - 但如果没有,它应该这样做。

另一种选择是

#ifdef WIN64

有细微差别的。 WIN64(不带前导下划线)由 SDK(或构建配置)定义。 由于这是由 SDK/build 配置定义的,因此它应该与 gcc 一起工作。

If you're compiling for the Windows platform, you should use:

#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.

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.

单身狗的梦 2024-07-12 20:52:55
#ifdef _LP64

适用于两个平台

#ifdef _LP64

Works on both platforms

简单 2024-07-12 20:52:55

如果您使用 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);
    }

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):

    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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文