centos64位编译32位程序

Posted mxly

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos64位编译32位程序相关的知识,希望对你有一定的参考价值。

test.c

#include <stdio.h>
int main()
{
        printf("sizeof long is %d
",sizeof(long));
        return 0;
}

64位编译

[[email protected] desktop]$ gcc test.c
[[email protected] 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
[[email protected] desktop]$

32位编译

[[email protected] desktop]$ gcc -m32 test.c

错误1:缺少32位头文件

技术分享图片

解决方法:

[[email protected] desktop]$ yum install glibc.i686

错误2:再次编译,发现编译成功,链接失败,提示如下错误

技术分享图片

解决方法:

先查看64位文件版本

[[email protected] 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

[[email protected] desktop]$ sudo rpm -ivh libgcc-4.8.5-28.el7.i686.rpm

安装完成后重新编译程序

[[email protected] desktop]$ gcc -m32 test.c
[[email protected] 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
[[email protected] desktop]$ a.out
sizeof long is 4
[[email protected] desktop]$

显示已经可以编译32位程序了。

以上是关于centos64位编译32位程序的主要内容,如果未能解决你的问题,请参考以下文章

试图加载格式不正确的程序

64位linux编译32位程序

编译 64 位 Linux 的 32 位 GTK+ 应用程序

ubuntu16 64位 编译64位程序和32位程序

第17章 LAMP环境搭建

第17章 LAMP环境搭建