如何检测我是否正在用 C++ 编译 64 位架构 [英] How can I detect if I'm compiling for a 64bits architecture in C++

查看:36
本文介绍了如何检测我是否正在用 C++ 编译 64 位架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C++ 函数中,如果编译器针对 64 位架构进行编译,我需要编译器选择不同的块.

In a C++ function I need the compiler to choose a different block if it is compiling for a 64 bit architecture.

我知道一种针对 MSVC++ 和 g++ 执行此操作的方法,因此我会将其发布为答案.但是我想知道是否有更好的方法(更优雅,适用于所有编译器/所有 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?

推荐答案

你为什么选择一个区块而不是另一个?如果您的决定是基于指针的大小,请使用 sizeof(void*) == 8.如果您的决定是基于整数的大小,请使用 sizeof(int) == 8.

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.

我的观点是架构本身的名称应该很少有任何区别.你只检查你需要检查的东西,为了你将要做的事情.你的问题没有很清楚地说明你检查的目的是什么.您所问的问题类似于尝试通过查询 Windows 版本来确定是否安装了 DirectX.您可以使用更多可移植和通用的工具.

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.

这篇关于如何检测我是否正在用 C++ 编译 64 位架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆