以下程序样例 foo.c 直接对比说明了 LP64 数据模型与 ILP32 数据模型的不同结果。同一个程序既可以编译为 32 位程序,也可以编译为 64 位程序。
#include <stdio.h>
int
main(int argc, char *argv[])
{
(void) printf("char is \t\t%lu bytes\n", sizeof (char));
(void) printf("short is \t%lu bytes\n", sizeof (short));
(void) printf("int is \t\t%lu bytes\n", sizeof (int));
(void) printf("long is \t\t%lu bytes\n", sizeof (long));
(void) printf("long long is \t\t%lu bytes\n", sizeof (long long));
(void) printf("pointer is \t%lu bytes\n", sizeof (void *));
return (0);
}
32 位编译的结果是:
% cc -O -o foo32 foo.c % foo32 char is 1 bytes short is 2 bytes int is 4 bytes long is 4 bytes long long is 8 bytes pointer is 4 bytes |
64 位编译的结果是:
% cc -xarch=generic64 -O -o foo64 foo.c % foo64 char is 1 bytes short is 2 bytes int is 4 bytes long is 8 bytes long long is 8 bytes pointer is 8 bytes |
缺省编译环境旨在最大化可移植性,即创建 32 位应用程序。