且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检测编译是32位还是64位?

更新时间:2021-09-20 02:31:21

steve yee写道:
steve yee wrote:

i想要检测源代码中的编译是32位还是64位

代码本身。所以不同的代码分别编译。怎么办

这个?
i want to detect if the compile is 32 bits or 64 bits in the source
code itself. so different code are compiled respectively. how to do
this?



没有便携方式。


有*便携式方法来检查算术范围

类型在编译时,如果那是你的不同代码需要。

使用< limits.hheader中定义的宏。


-

Eric Sosman
es*****@acm-dot-org.inva


steve yee发布:
steve yee posted:

i想要检测源代码中的编译是32位还是64位

代码本身。所以不同的代码分别编译。怎么办

这个?
i want to detect if the compile is 32 bits or 64 bits in the source
code itself. so different code are compiled respectively. how to do
this?



如果您正在编写可移植代码,则不需要。


尽管如此,您可以尝试一些东西喜欢:


#include< limits.h>


/ * inttype_MAX或任何(1<<< k)-1,其中0
* /

#define IMAX_BITS(m)((m)/((m)%0x3fffffffL + 1)/ 0x3fffffffL%0x3fffffffL *

30 \

+(m)%0x3fffffffL /((m)%31 + 1)/ 31%31 * 5 + 4-12 /((m)%31 + 3))


#if IMAX_BITS(UINT_MAX)== 32

#define BIT32

#else

#if IMAX_BITS(UINT_MAX)== 64

#define BIT64

#else

#error"系统必须是32 -Bit或64-bit。"

#endif

#endif


int main(无效)

{

/ *这是我的非便携代码* /

}


-


Frederick Gotham


You shouldn''t need to if you''re writing portable code.

Nonetheless, you could try something like:

#include <limits.h>

/* Number of bits in inttype_MAX, or in any (1<<k)-1 where 0 <= k < 3.2E+10
*/
#define IMAX_BITS(m) ((m) /((m)%0x3fffffffL+1) /0x3fffffffL %0x3fffffffL *
30 \
+ (m)%0x3fffffffL /((m)%31+1)/31%31*5 + 4-12/((m)%31+3))

#if IMAX_BITS(UINT_MAX) == 32
#define BIT32
#else
#if IMAX_BITS(UINT_MAX) == 64
#define BIT64
#else
#error "System must be either 32-Bit or 64-Bit."
#endif
#endif

int main(void)
{
/* Here''s my non-portable code */
}

--

Frederick Gotham


steve yee写道:
steve yee wrote:

i想要在源代码中检测编译是32位还是64位

代码本身。所以不同的代码分别编译。怎么办

这个?
i want to detect if the compile is 32 bits or 64 bits in the source
code itself. so different code are compiled respectively. how to do
this?



你邀请bug,以及关于
$ b $主题的问题b标准C,如果你编写了必须改变的源代码。通常的

问题是关于(sizeof)<各种指针类型>。如果您的问题仅仅是(sizeof)int,那么您可以通过假设它是否具有32位或64位平台来确定问题来创建问题。如果你坚持使用指针和整数联盟的
,知道它是32位还是64位

不会拯救你。更重要的是,如果你是编写代码的人之一,那么

取决于(sizeof)(size_t x)。

You invite bugs, as well as taking the question off the topic of
standard C, if you write source code which has to change. The usual
question is about (sizeof)<various pointer types>. If your question is
simply about (sizeof)int, you create problems by assuming it is
determined by whether you have a 32- or 64-bit platform. If you insist
on unions of pointers and ints, knowing whether it is 32 or 64 bits
won''t save you. More so, if you are one of those who writes code which
depends on (sizeof)(size_t x).