在64位centos6上編譯32位的匯編程序,如果程序中使用了C庫,比如printf。因為是編譯32位的目標程序,所以使用gcc編譯的時候需要加上-m32選項,但是如果編譯的話會報錯,以print.s程序為例子
1 .code32 2 .section .data 3 output: 4 .asciz "hello gas '%d'" 5 6 .section .text 7 .globl main 8 main: 9 pushl $4 10 pushl $output 11 call printf 12 addl $8, %esp 13 14 pushl $0 15 call exit
使用gcc -g -m32 -o print print.s編譯的時候,會報錯:
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/4.4.7/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/4.4.7/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: skipping incompatible /lib/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
從編譯錯誤基本可以看出來是因為沒有找到gcc_s,在沒有找到gcc_s之前的錯誤意思是系統中有gcc_s lib庫,不過不匹配! 通過查詢之后發現是因為64位系統使用的是64位的gcc_s庫,我們目標程序是32位的,所以需要32位的gcc_s,經過一番查找之后,在http://pkgs.org/centos-6/centos-i386/libgcc-4.4.7-11.el6.i686.rpm.html目錄下找到了適合版本的gcc_s 32位庫,然后使用rpm安裝這個包。
然后再編譯程序,使用之前的指令,程序正常運行