The method recorded here theoretically applies to all RedHat‑based Linux distributions!
Normally on 64bit Linux (after installing gcc and related tools), compiled executables target the 64bit environment. If you want to compile for 32bit (which can also run on 64bit systems), just add the -m32 option to gcc (for ld there is a corresponding -melf_i386). Although compiling only requires adding the parameter, you still need to install the 32bit versions of the C and C++ standard libraries.
For example, if you compile directly with -m32, you will get the following error:
[kres@localhost test6432]$ gcc -m32 test.c
In file included from /usr/include/features.h:399:0,
from /usr/include/unistd.h:25,
from /usr/include/usb.h:28,
from test.c:1:
/usr/include/gnu/stubs.h:7:27: fatal error: gnu/stubs-32.h:No such file or directory
# include ;
^
compilation terminated.
Next you need to install the i686 standard libraries (CentOS 7 uses i686, older versions use i386 etc.)
sudo yum install glibc-devel.i686
After execution, the following packages are installed:
Installing:
glibc-devel i686 2.17-78.el7 base 1.0 M
Installing for dependencies:
glibc i686 2.17-78.el7 base 4.2 M
nss-softokn-freebl i686 3.16.2.3-13.el7_1 updates 187 k
Updating for dependencies:
nss-softokn-freebl x86_64 3.16.2.3-13.el7_1 updates 204 k
After installation, running gcc again produces the following error:
[kres@localhost test6432]$ gcc -m32 test.c
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/4.8.3/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1
You still need to install related libraries, run:
sudo yum install libstdc++-devel.i686
After execution, the following packages are installed:
Installing:
libstdc++-devel i686 4.8.3-9.el7 base 1.5 M
Installing for dependencies:
libgcc i686 4.8.3-9.el7 base 99 k
libstdc++ i686 4.8.3-9.el7 base 307 k
After completion, compiling again succeeds. Using file on a.out shows the comparison between 32bit and 64bit executables:
a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0x6788cf8e9553f2e948ac8ecd51891bdbdab7e076, not stripped
a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0x5921d6f995d0d4668cde23339558fb2db926e622, not stripped
Reference article:
- HowTo Compile a 32-bit Application Using gcc On the 64-bit Linux Version – http://www.cyberciti.biz/tips/compile-32bit-application-using-gcc-64-bit-linux.html
博主友情提示:
如您在评论中需要提及如QQ号、电子邮件地址或其他隐私敏感信息,欢迎使用>>博主专用加密工具v3<<处理后发布,原文只有博主可以看到。