2011-12-21 44 views

回答

2

這裏有一個作弊的方式排序:

#if defined(__i386__) 
    return "i386"; 
#elif defined(__x86_64__) 
    return "x86-64"; 
#elif defined(__arm__) 
    return "arm"; 
// etc... 
#else 
# error "Unknown architecture!" 
#endif 

這不是絕對必要的檢查在運行時,因爲有一個爲每個架構一個單獨的可執行映像。

+0

通過定義爲編譯器或庫使用保留的標識符,使用'__foo__'不能在不同的編譯器中移植。 – moshbear 2011-12-21 20:47:49

+1

它適用於gcc和clang,但是,這對於OS X上的大多數實際用途已足夠。 – duskwuff 2011-12-21 20:58:47

0

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 ('\0'). 

提示: uname -m輸出返回(struct utsname).machine