centos64位编译32位程序
test.c
#include <stdio.h> int main() { printf("sizeof long is %d\n",sizeof(long)); return 0; }
64位编译
[swdn@dev desktop]$ gcc test.c
[swdn@dev desktop]$ file a.out
a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not stripped
[swdn@dev desktop]$
32位编译
[swdn@dev desktop]$ gcc -m32 test.c
错误1:缺少32位头文件
解决方法:
[swdn@dev desktop]$ yum install glibc.i686
错误2:再次编译,发现编译成功,链接失败,提示如下错误
解决方法:
先查看64位文件版本
[swdn@dev desktop]$ yum list libgcc 已加载插件:fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com 已安装的软件包 libgcc.x86_64 4.8.2-8.el6 @hop5
由于yum提供的32版本低于64位的版本,使用yum install libgcc.i686会安装失败,因此需自己下载32位版本进行安装
下载地址:https://pkgs.org/download/libgcc
安装libgcc.i686
[swdn@dev desktop]$ sudo rpm -ivh libgcc-4.8.5-28.el7.i686.rpm
安装完成后重新编译程序
[swdn@dev desktop]$ gcc -m32 test.c [swdn@dev desktop]$ file a.out a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not stripped [swdn@dev desktop]$ a.out sizeof long is 4 [swdn@dev desktop]$
显示已经可以编译32位程序了。