先写个hello world的简单程序
gcc -m32 a.c
先确保a.c能够编译链接成功,再去编译你的32位程序。
[root@localhost ~]# gcc -m32 hello.c
In file included from /usr/include/features.h:399:0,
from /usr/include/stdio.h:27,
from hello.c:1:
/usr/include/gnu/stubs.h:7:27: fatal error: gnu/stubs-32.h: No such file or directory
# include <gnu/stubs-32.h>
^
compilation terminated.
[root@localhost ~]#
编译失败,缺少32位头文件。
使用yum安装i686的库
[root@localhost centos-release]# yum list glibc
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Installed Packages
glibc.i686 2.17-55.el7 @base
glibc.x86_64 2.17-55.el7 @anaconda
[root@localhost centos-release]# yum install glibc.i686
yum安装时有个小插曲,提示GPG keys配置有问题:
The GPG keys listed for the "CentOS-7 - Base - 163.com" repository are already installed but they are not correct for this package.
Check that the correct key URLs are configured for this repository.
解决方法:
手动从163网站导入CentOS-7的key,我的版本是CentOS 7。
rpm -import http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7
安装glibc开发库:
[root@localhost ~]# yum list *glibc*
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Installed Packages
compat-glibc.x86_64 1:2.12-4.el7.centos @anaconda
compat-glibc-headers.x86_64 1:2.12-4.el7.centos @anaconda
glibc.i686 2.17-55.el7 @base
glibc.x86_64 2.17-55.el7 @anaconda
glibc-common.x86_64 2.17-55.el7 @anaconda
glibc-devel.x86_64 2.17-55.el7 @anaconda
glibc-headers.x86_64 2.17-55.el7 @anaconda
Available Packages
glibc-devel.i686 2.17-55.el7 base
glibc-static.i686 2.17-55.el7 base
glibc-static.x86_64 2.17-55.el7 base
glibc-utils.x86_64 2.17-55.el7 base
[root@localhost ~]# yum install glibc-devel.i686
再次编译,发现编译成功,但是链接失败。
[root@localhost ~]# gcc -m32 hello.c
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/4.8.2/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status
[root@localhost ~]#
缺少动态库libgcc
[root@localhost ~]# yum list libgcc
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Installed Packages
libgcc.x86_64 4.8.2-16.el7 @anaconda
Available Packages
libgcc.i686 4.8.2-16.el7 base
[root@localhost ~]# yum install libgcc.i686
libgcc安装成功后,再次编译
[root@localhost ~]# gcc -m32 hello.c
[root@localhost ~]# ./a.out
hello world
发现编程链接全部成功,OK,大功告成。