https://access.redhat.com/solutions/4887
How to compile a 32-bit C application using gcc on the 64-bit version of Red Hat Enterprise Linux?
SOLUTION 已验证 - 已更新 2015年十一月26日19:18 -
环境
- Red Hat Enterprise Linux (all versions)
问题
- How to compile a 32-bit C application using
gcc
on the 64-bit version ofRed Hat Enterprise Linux
?
决议
-
To compile a 32-bit C application for
Red Hat Enterprise Linux
running on a 64-bit platform, use thegcc
option-m32
.- According to the
man
page forgcc
, the-m32
option sets int, long, and pointer variables to 32 bits (4 bytes). The-m64
option sets int to 32 bits (4 bytes) and long & pointer to 64 bits (8 bytes).
- According to the
-
The following segment of code demonstrates this:
#include <stdio.h> int main() { int i; long l; char *p; printf("Integer size: %i bytes\\n", sizeof(i)); printf("Long Integer size: %i bytes\\n", sizeof(l)); printf("Pointer size: %i bytes\\n", sizeof(p)); return 0; }
-
The following is a comparison of the output when compiled with the
-m32
and-m64
options:# gcc -o ./test.64 -m64 test.c # gcc -o ./test.32 -m32 test.c # # ./test.64 Integer size: 4 bytes Long Integer size: 8 bytes Pointer size: 8 bytes # # ./test.32 Integer size: 4 bytes Long Integer size: 4 bytes Pointer size: 4 bytes
NOTE: in order to get correct results when compiling applications, make sure that the libraries linked against are installed in 32-bit versions. As an example, the packages
glibc
,glibc-devel
andlibgcc
are needed for most C applications. -
To install the 32-bit versions on
Red Hat Enterprise Linux 5
:# yum install libgcc.i386 glibc.i686 glibc-devel.i386
-
On
Red Hat Enterprise Linux 6
and7
:# yum install libgcc.i686 glibc.i686 glibc-devel.i686
-
On older versions of
Red Hat Enterprise Linux
the same should be done withup2date
:# up2date --arch=i386 --install glibc-devel libgcc