这里首先用64位系统编译32位代码 GCC -m32 test.c的编译命令,发现编译不通过
下面是 RedHat系列:Redhat、Centos、Fedora
而Debian系列:Debian、Ubuntu在帖子下方也有编译方法
先写个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
原创:
要编译 g++ -m32 就需要安装此处
[root@localhost ~]#yum install libstdc++-static.i686
[root@localhost ~]#yum install libstdc++.i686
https://blog.csdn.net/meteor5118/article/details/37875617
Debian系列:Debian、Ubuntu等
一 .查看当前linux系统到底是多少位!!!
方法一:
getconf LONG_BIT
方法二:
我觉得这是最简单的东西了:也好记:
uname -a
方法三:
上面的一切都说明我的系统是64位:
好,接下来我们需要做的就是配置,让它也可以进行32位的编译
(插曲)
在安装 Ubuntu 的时候建立的帐户 sai,想把密码改成两个字母aa,方便输入。
运行终端
sai@xmomx:~$ passwd sai
更改 sai 的密码。
(当前)UNIX 密码: xx
输入新的 UNIX 密码: aa
重新输入新的 UNIX 密码:aa
必须选择更长的密码
输入新的 UNIX 密码: 1234567
重新输入新的 UNIX 密码: 1234567
Bad: new password is too simple
输入新的 UNIX 密码:
怎么办?加上su权限执行即可。这样来做:
方案一:
sai@xmomx:~$ sudo passwdsai
[sudo] password for sai: xxx
输入新的 UNIX 密码: aa
重新输入新的 UNIX 密码: aa
passwd:已成功更新密码
假如我们并没有配置,就直接用 的话,会报错
程序1:
#include <stdio.h>
struct stu{
char a;
double b;
char c;
};
int main(){
struct stu ss;
printf("%d\n", sizeof(ss));
return 0;
}
我们先看一下,在64位下运行的结果:
然后我们再看一下,在32位下运行的结果(我们直接用,也就是说是还没有配置,如:gcc test.c -m32),回报错:
好,接下来,到啦我们该配置的时候来:
步骤二(配置多构架内核,也就是同时支持64,32位)
(1),确认打开多构架
(2),打开多构架支持功能
sudo dpkg --add-architecture i386
sudo apt-get update
sudo apt-get dist-upgrade
依次执行上面的命令
另外第三个命令执行时间有点长,(哈哈,够大家去吃个饭,冲个coffee了,大家耐心等待吧!!!)
当我们敲完上面的命令时就说明,已经支持一部分了,但是现在,我们的程序还不能完全运行
接着我们还要输入如下命令:
sudo apt-get install gcc-multilib g++-multilib
这时我们的编译器已经完美了,也就是说用这个命令(gcc test.c -m32)
大家可以看一下我们程序运行的结果:
好了,0k,我们的编译器完成了我们的目标了。