是否有一种标准方法可以在编译时确定系统是 32 位还是 64 位? [英] Is there a standard way to determine at compile-time if system is 32 or 64 bit?
问题描述
我需要设置 #ifdef
- 检查条件编译.我想自动化该过程,但无法指定目标操作系统/机器.有什么办法可以让预编译器确定它是在 32 位还是 64 位上运行?
I need to set #ifdef
- checks for conditional compile. I want to automate the process but cannot specify the target OS/machine. Is there some way that the pre-compiler can resolve whether it it is running on 32-bit or 64-bit?
(解释)我需要定义一个大小为 64 位的类型.在 64 位操作系统上它是一个 long,在大多数其他操作系统上它是一个 long long.
(Explanation) I need to define a type that is 64 bits in size. On 64bit OS it is a long, on most others it is a long long.
我找到了这个答案 - 这是正确的方法吗?去吗?
I found this answer - is this the correct way to go?
推荐答案
唯一可以可靠地进行的编译检查是 sizeof(void*) == 8
,x64 为 true,x86 为 false.这是一个 constexpr,您可以将它传递给模板,但您可以忘记将 ifdef 与它一起使用.没有独立于平台的方法来了解目标架构的地址大小(在预处理时),您需要向您的 IDE 询问.标准甚至没有地址大小的概念.
The only compile check you can do reliably would be sizeof(void*) == 8
, true for x64 and false for x86. This is a constexpr and you can pass it to templates but you can forget using ifdef with it. There is no platform-independent way to know the address size of the target architecture (at pre-process time), you will need to ask your IDE for one. The Standard doesn't even have the concept of the address size.
这篇关于是否有一种标准方法可以在编译时确定系统是 32 位还是 64 位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!