Ngày nay trình biên dịch mặc định là phiên bản 64-bit. Đôi khi cần biên dịch và chạy mã trên hệ thống 32-bit. Lúc đó phải dùng tính năng này.
Trước hết, kiểm tra phiên bản mục tiêu (target) hiện tại của gcc. Gõ lệnh:
gcc –v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu ........... ........... ...........
Ở đây Target là x86_64, nghĩa là đang dùng gcc 64-bit. Để dùng hệ thống 32-bit, gõ lệnh:
gcc –m32 program_name.c
Đôi khi lệnh này báo lỗi như sau. Điều này cho thấy thư viện chuẩn của gcc bị thiếu. Cần cài đặt chúng.
In file included from test_c.c:1:0: /usr/include/stdio.h:27:10: fatal error: bits/libc-header-start.h: No such file or directory #include <bits/libc-header-start.h> ^~~~~~~~~~~~~~~~~~~~~~~~~~ compilation terminated.
Để cài đặt thư viện chuẩn cho gcc, gõ các lệnh:
sudo apt-get install gcc-multilib sudo apt-get install g++-multilib
Dưới đây là ví dụ thấy sự khác biệt khi chạy trên 32-bit và 64-bit.
Ví dụ
#include <stdio.h>
main() {
printf("The Size is: %lu\n", sizeof(long));
}
Kết quả (64-bit)
$ gcc test_c.c
test_c.c:3:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
main(){
^~~~
$ ./a.out
The Size is: 8
Kết quả (32-bit)
$ gcc -m32 test_c.c
test_c.c:3:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
main(){
^~~~
test_c.c: In function ‘main’:
test_c.c:4:28: warning: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument 2 has type ‘unsigned int’ [-Wformat=]
printf("The Size is: %lu\n", sizeof(long));
~~^ %u
$ ./a.out
The Size is: 4