ในปัจจุบันคอมไพเลอร์มักเป็นเวอร์ชัน 64-bit แบบเริ่มต้น บางครั้งต้องคอมไพล์และรันโค้ดบนระบบ 32-bit ต้องใช้ฟีเจอร์นี้
ก่อนอื่น ตรวจสอบเวอร์ชันเป้าหมายปัจจุบันของ gcc compiler:
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 ........... ........... ...........
แสดงว่า Target เป็น x86_64 ใช้ gcc 64-bit เพื่อใช้ระบบ 32-bit ใช้คำสั่ง:
gcc –m32 program_name.c
บางครั้งอาจเกิดข้อผิดพลาด แสดงว่า standard library ของ gcc หายไป ต้องติดตั้ง:
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.
ติดตั้ง standard library สำหรับ gcc:
sudo apt-get install gcc-multilib sudo apt-get install g++-multilib
โค้ดตัวอย่างเพื่อดูความแตกต่างระหว่าง 32-bit และ 64-bit:
ตัวอย่าง
#include <stdio.h>
main() {
printf("The Size is: %lu\n", sizeof(long));
}
ผลลัพธ์ (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
ผลลัพธ์ (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