如何在OS X中使用标准C库或Cocoa获取CPU架构字符串?
uname(3)
这里有一个作弊的方法:
#if defined(__i386__)
return "i386";
#elif defined(__x86_64__)
return "x86-64";
#elif defined(__arm__)
return "arm";
// etc...
#else
# error "Unknown architecture!"
#endif
没有必要在运行时检查,因为每个体系结构都有一个单独的可执行映像。
From man 2 uname
:
#include <sys/utsname.h>
int uname(struct utsname *buf);
uname() returns system information in the structure pointed to by buf.
The utsname struct is defined in
<sys/utsname.h>:
struct utsname {
char sysname[]; /* Operating system name (e.g., "Linux") */
char nodename[]; /* Name within "some implementation-defined
network" */
char release[]; /* OS release (e.g., "2.6.28") */
char version[]; /* OS version */
char machine[]; /* Hardware identifier */
};
The length of the arrays in a struct utsname is unspecified;
the fields are terminated by a null byte (' ').
提示:uname -m返回(struct utsname).machine