c++怎么获取linux处理器架构是X86_64还是ia64

时间:2022-12-29 03:14:37
linux c++ 处理器 架构

9 个解决方案

#1


linux   c++代码

#2


XXX@ubuntu:~$ getconf  LONG_BIT
32

如果结果是64就是X86_64

#3


uname -a 其实也可以看到。

#4


X86_64 和 ia64  什么区别?

楼主是不是想在 32位和 64位上运行,我觉得这种情况不是把2种架构的编入一个程序,在运行时选择;而是应该在编译时分别编译 32位和 64位 的版本,加一个宏定义条件编译 

#ifdef IA_64
 ....
#else
....
#endif 

#5


http://nadeausoftware.com/articles/2012/02/c_c_tip_how_detect_processor_type_using_compiler_predefined_macros


Processor macros are predefined by all C/C++ compilers to enable #if/#endif sets to wrap processor-specific code, such as in-line assembly for SSE instructions on x86 processors.  But be aware that there are no standards for processor macros. The same compiler may have different macros on different operating systems, and different compilers for the same processor may have different macros. Always check your compiler's manual first...

Itanium

A.K.A.: IA64
Developer: Intel
Processors: Itanium, Itanium 2, Itanium 2 9000/9100/9300, etc.

#if defined(__ia64) || defined(__itanium__) || defined(_M_IA64)
/* Itanium -------------------------------------------------- */

#endif


x86 and x86-64
A.K.A. (32-bit): IA-32, i386, x86, x86-32
A.K.A. (64-bit): AMD64, EM64T, IA-32e, Intel64, x64, x86-64
Developers: AMD, Intel
Processors: Athlon, Atom, Core, Core 2, Core i3/i5/i7, Opteron, Pentium, Phenom, Sempron, Turion, etc.

#if defined(__x86_64__) || defined(_M_X64)
/* x86 64-bit ----------------------------------------------- */

#elif defined(__i386) || defined(_M_IX86)
/* x86 32-bit ----------------------------------------------- */

#endif


#6


编译器会有一些内置的宏供你使用,如果不想依赖于某种特定的编译器,可以自己在makefile中读取机器类型,然后自己定义宏实现不同位长机器上的兼容。

#7


楼主的问题个人浅见分两方面来看
如果讨论的是ia64 native 的binary,那么编译生成的指令集根本就是不兼容的,所以简单在compile时候设个flag就可以了
如果讨论的是ia64 instruction emulation, 也就是原始的x86的binary不经改动在安腾上运行,那么比较可靠的办法就是cpuid,然后看ia64 capability 位,看是否是itanium family 当然运行在ia32 emulation mode
当然cpuid对第一种情况也适用,只是似乎没有必要舍近求远而已
仅供楼主参考

#8


uname -a
getconf LONG_BIT

#9


谢谢 楼上的各位  用了一个宏__x86_64__解决了  
其实我想实现的是  
#ifdef  __x86_64__
a =1;
#else
a =2;

#1


linux   c++代码

#2


XXX@ubuntu:~$ getconf  LONG_BIT
32

如果结果是64就是X86_64

#3


uname -a 其实也可以看到。

#4


X86_64 和 ia64  什么区别?

楼主是不是想在 32位和 64位上运行,我觉得这种情况不是把2种架构的编入一个程序,在运行时选择;而是应该在编译时分别编译 32位和 64位 的版本,加一个宏定义条件编译 

#ifdef IA_64
 ....
#else
....
#endif 

#5


http://nadeausoftware.com/articles/2012/02/c_c_tip_how_detect_processor_type_using_compiler_predefined_macros


Processor macros are predefined by all C/C++ compilers to enable #if/#endif sets to wrap processor-specific code, such as in-line assembly for SSE instructions on x86 processors.  But be aware that there are no standards for processor macros. The same compiler may have different macros on different operating systems, and different compilers for the same processor may have different macros. Always check your compiler's manual first...

Itanium

A.K.A.: IA64
Developer: Intel
Processors: Itanium, Itanium 2, Itanium 2 9000/9100/9300, etc.

#if defined(__ia64) || defined(__itanium__) || defined(_M_IA64)
/* Itanium -------------------------------------------------- */

#endif


x86 and x86-64
A.K.A. (32-bit): IA-32, i386, x86, x86-32
A.K.A. (64-bit): AMD64, EM64T, IA-32e, Intel64, x64, x86-64
Developers: AMD, Intel
Processors: Athlon, Atom, Core, Core 2, Core i3/i5/i7, Opteron, Pentium, Phenom, Sempron, Turion, etc.

#if defined(__x86_64__) || defined(_M_X64)
/* x86 64-bit ----------------------------------------------- */

#elif defined(__i386) || defined(_M_IX86)
/* x86 32-bit ----------------------------------------------- */

#endif


#6


编译器会有一些内置的宏供你使用,如果不想依赖于某种特定的编译器,可以自己在makefile中读取机器类型,然后自己定义宏实现不同位长机器上的兼容。

#7


楼主的问题个人浅见分两方面来看
如果讨论的是ia64 native 的binary,那么编译生成的指令集根本就是不兼容的,所以简单在compile时候设个flag就可以了
如果讨论的是ia64 instruction emulation, 也就是原始的x86的binary不经改动在安腾上运行,那么比较可靠的办法就是cpuid,然后看ia64 capability 位,看是否是itanium family 当然运行在ia32 emulation mode
当然cpuid对第一种情况也适用,只是似乎没有必要舍近求远而已
仅供楼主参考

#8


uname -a
getconf LONG_BIT

#9


谢谢 楼上的各位  用了一个宏__x86_64__解决了  
其实我想实现的是  
#ifdef  __x86_64__
a =1;
#else
a =2;